1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package sha1 implements the SHA1 hash algorithm as defined in RFC 3174.
14 crypto
.RegisterHash(crypto
.SHA1
, New
)
17 // The size of a SHA1 checksum in bytes.
20 // The blocksize of SHA1 in bytes.
32 // digest represents the partial evaluation of a checksum.
40 func (d
*digest
) Reset() {
50 // New returns a new hash.Hash computing the SHA1 checksum.
51 func New() hash
.Hash
{
57 func (d
*digest
) Size() int { return Size
}
59 func (d
*digest
) BlockSize() int { return BlockSize
}
61 func (d
*digest
) Write(p
[]byte) (nn
int, err error
) {
65 n
:= copy(d
.x
[d
.nx
:], p
)
74 n
:= len(p
) &^ (chunk
- 1)
79 d
.nx
= copy(d
.x
[:], p
)
84 func (d0
*digest
) Sum(in
[]byte) []byte {
85 // Make a copy of d0 so that caller can keep writing and summing.
88 return append(in
, hash
[:]...)
91 func (d
*digest
) checkSum() [Size
]byte {
93 // Padding. Add a 1 bit and 0 bits until 56 bytes mod 64.
97 d
.Write(tmp
[0 : 56-len%64
])
99 d
.Write(tmp
[0 : 64+56-len%64
])
104 for i
:= uint(0); i
< 8; i
++ {
105 tmp
[i
] = byte(len >> (56 - 8*i
))
113 var digest
[Size
]byte
114 for i
, s
:= range d
.h
{
115 digest
[i
*4] = byte(s
>> 24)
116 digest
[i
*4+1] = byte(s
>> 16)
117 digest
[i
*4+2] = byte(s
>> 8)
118 digest
[i
*4+3] = byte(s
)
124 // Sum returns the SHA1 checksum of the data.
125 func Sum(data
[]byte) [Size
]byte {