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
|
|
|
|
|
2009-05-20 19:16:38 -06:00
|
|
|
import (
|
|
|
|
"hash";
|
|
|
|
"os";
|
|
|
|
)
|
|
|
|
|
|
|
|
// The size of a SHA1 checksum in bytes.
|
|
|
|
const Size = 20;
|
2008-11-24 13:30:40 -07:00
|
|
|
|
|
|
|
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-05-20 19:16:38 -06:00
|
|
|
// digest represents the partial evaluation of a checksum.
|
|
|
|
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-05-20 19:16:38 -06:00
|
|
|
func (d *digest) Reset() {
|
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;
|
2009-05-22 23:40:26 -06:00
|
|
|
d.nx = 0;
|
|
|
|
d.len = 0;
|
2009-05-20 19:16:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a Hash computing the SHA1 checksum.
|
|
|
|
func New() hash.Hash {
|
|
|
|
d := new(digest);
|
|
|
|
d.Reset();
|
2008-11-24 13:30:40 -07:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2009-05-20 19:16:38 -06:00
|
|
|
func (d *digest) Size() int {
|
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2009-04-15 21:27:45 -06:00
|
|
|
_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-05-20 19:16:38 -06: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;
|
|
|
|
}
|
|
|
|
|