1 // Copyright 2010 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 ripemd160 implements the RIPEMD-160 hash algorithm.
6 package ripemd160 // import "golang.org/x/crypto/ripemd160"
8 // RIPEMD-160 is designed by by Hans Dobbertin, Antoon Bosselaers, and Bart
9 // Preneel with specifications available at:
10 // http://homes.esat.kuleuven.be/~cosicart/pdf/AB-9601/AB-9601.pdf.
18 crypto.RegisterHash(crypto.RIPEMD160, New)
21 // The size of the checksum in bytes.
24 // The block size of the hash algorithm in bytes.
35 // digest represents the partial evaluation of a checksum.
37 s [5]uint32 // running context
38 x [BlockSize]byte // temporary buffer
39 nx int // index into x
40 tc uint64 // total count of bytes processed
43 func (d *digest) Reset() {
44 d.s[0], d.s[1], d.s[2], d.s[3], d.s[4] = _s0, _s1, _s2, _s3, _s4
49 // New returns a new hash.Hash computing the checksum.
50 func New() hash.Hash {
56 func (d *digest) Size() int { return Size }
58 func (d *digest) BlockSize() int { return BlockSize }
60 func (d *digest) Write(p []byte) (nn int, err error) {
65 if n > BlockSize-d.nx {
68 for i := 0; i < n; i++ {
72 if d.nx == BlockSize {
81 d.nx = copy(d.x[:], p)
86 func (d0 *digest) Sum(in []byte) []byte {
87 // Make a copy of d0 so that caller can keep writing and summing.
90 // Padding. Add a 1 bit and 0 bits until 56 bytes mod 64.
95 d.Write(tmp[0 : 56-tc%64])
97 d.Write(tmp[0 : 64+56-tc%64])
102 for i := uint(0); i < 8; i++ {
103 tmp[i] = byte(tc >> (8 * i))
111 var digest [Size]byte
112 for i, s := range d.s {
113 digest[i*4] = byte(s)
114 digest[i*4+1] = byte(s >> 8)
115 digest[i*4+2] = byte(s >> 16)
116 digest[i*4+3] = byte(s >> 24)
119 return append(in, digest[:]...)