2008-11-24 13:30:40 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2009-03-07 17:56:21 -07:00
|
|
|
// This package implements the SHA1 hash algorithm as defined in RFC 3174.
|
2008-11-24 13:30:40 -07:00
|
|
|
package sha1
|
|
|
|
|
|
|
|
import "os"
|
|
|
|
|
|
|
|
const (
|
2009-01-16 11:14:28 -07:00
|
|
|
_Chunk = 64;
|
|
|
|
|
|
|
|
_Init0 = 0x67452301;
|
|
|
|
_Init1 = 0xEFCDAB89;
|
|
|
|
_Init2 = 0x98BADCFE;
|
|
|
|
_Init3 = 0x10325476;
|
|
|
|
_Init4 = 0xC3D2E1F0;
|
2008-11-24 13:30:40 -07:00
|
|
|
)
|
|
|
|
|
2009-03-07 17:56:21 -07:00
|
|
|
// Digest represents the partial evaluation of a checksum.
|
2009-01-20 15:40:40 -07:00
|
|
|
type Digest struct {
|
2008-11-24 13:30:40 -07:00
|
|
|
h [5]uint32;
|
2009-01-16 11:14:28 -07:00
|
|
|
x [_Chunk]byte;
|
2008-11-24 13:30:40 -07:00
|
|
|
nx int;
|
|
|
|
len uint64;
|
|
|
|
}
|
|
|
|
|
2009-03-07 17:56:21 -07:00
|
|
|
// NewDigest creates a new Digest.
|
2009-01-20 15:40:40 -07:00
|
|
|
func NewDigest() *Digest {
|
2009-01-06 16:19:02 -07:00
|
|
|
d := new(Digest);
|
2009-01-16 11:14:28 -07:00
|
|
|
d.h[0] = _Init0;
|
|
|
|
d.h[1] = _Init1;
|
|
|
|
d.h[2] = _Init2;
|
|
|
|
d.h[3] = _Init3;
|
|
|
|
d.h[4] = _Init4;
|
2008-11-24 13:30:40 -07:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2009-01-16 11:14:28 -07:00
|
|
|
func _Block(dig *Digest, p []byte) int
|
2008-11-24 13:30:40 -07:00
|
|
|
|
2009-03-07 17:56:21 -07:00
|
|
|
// Write updates the Digest with the incremental checksum generated by p.
|
|
|
|
// It returns the number of bytes written; err is always nil.
|
2008-12-18 23:37:22 -07:00
|
|
|
func (d *Digest) Write(p []byte) (nn int, err *os.Error) {
|
2008-11-24 13:30:40 -07:00
|
|
|
nn = len(p);
|
|
|
|
d.len += uint64(nn);
|
|
|
|
if d.nx > 0 {
|
|
|
|
n := len(p);
|
2009-01-16 11:14:28 -07:00
|
|
|
if n > _Chunk-d.nx {
|
|
|
|
n = _Chunk-d.nx;
|
2008-11-24 13:30:40 -07:00
|
|
|
}
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
d.x[d.nx+i] = p[i];
|
|
|
|
}
|
|
|
|
d.nx += n;
|
2009-01-16 11:14:28 -07:00
|
|
|
if d.nx == _Chunk {
|
|
|
|
_Block(d, d.x);
|
2008-11-24 13:30:40 -07:00
|
|
|
d.nx = 0;
|
|
|
|
}
|
|
|
|
p = p[n:len(p)];
|
|
|
|
}
|
2009-01-16 11:14:28 -07:00
|
|
|
n := _Block(d, p);
|
2008-11-24 13:30:40 -07:00
|
|
|
p = p[n:len(p)];
|
|
|
|
if len(p) > 0 {
|
|
|
|
for i := 0; i < len(p); i++ {
|
|
|
|
d.x[i] = p[i];
|
|
|
|
}
|
|
|
|
d.nx = len(p);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-03-07 17:56:21 -07:00
|
|
|
// Sum returns the SHA-1 checksum of the data written to the Digest
|
|
|
|
// in the form of an array of 20 bytes in big-endian order.
|
2008-12-18 23:37:22 -07:00
|
|
|
func (d *Digest) Sum() []byte {
|
2008-11-24 13:30:40 -07:00
|
|
|
// Padding. Add a 1 bit and 0 bits until 56 bytes mod 64.
|
|
|
|
len := d.len;
|
|
|
|
var tmp [64]byte;
|
|
|
|
tmp[0] = 0x80;
|
|
|
|
if len%64 < 56 {
|
2008-12-18 23:37:22 -07:00
|
|
|
d.Write(tmp[0:56-len%64]);
|
2008-11-24 13:30:40 -07:00
|
|
|
} else {
|
2008-12-18 23:37:22 -07:00
|
|
|
d.Write(tmp[0:64+56-len%64]);
|
2008-11-24 13:30:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Length in bits.
|
|
|
|
len <<= 3;
|
|
|
|
for i := uint(0); i < 8; i++ {
|
|
|
|
tmp[i] = byte(len>>(56-8*i));
|
|
|
|
}
|
2008-12-18 23:37:22 -07:00
|
|
|
d.Write(tmp[0:8]);
|
2008-11-24 13:30:40 -07:00
|
|
|
|
|
|
|
if d.nx != 0 {
|
|
|
|
panicln("oops");
|
|
|
|
}
|
|
|
|
|
2009-01-06 16:19:02 -07:00
|
|
|
p := make([]byte, 20);
|
2008-11-24 13:30:40 -07:00
|
|
|
j := 0;
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
s := d.h[i];
|
|
|
|
p[j] = byte(s>>24); j++;
|
|
|
|
p[j] = byte(s>>16); j++;
|
|
|
|
p[j] = byte(s>>8); j++;
|
|
|
|
p[j] = byte(s); j++;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|