OSDN Git Service

6e88521c3f6c58c7afee82d390e4c04a868bd90b
[pf3gnuchains/gcc-fork.git] / libgo / go / crypto / ripemd160 / ripemd160.go
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.
4
5 // This package implements the RIPEMD-160 hash algorithm.
6 package ripemd160
7
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.
11
12 import (
13         "crypto"
14         "hash"
15         "os"
16 )
17
18 func init() {
19         crypto.RegisterHash(crypto.RIPEMD160, New)
20 }
21
22 // The size of the checksum in bytes.
23 const Size = 20
24
25 // The block size of the hash algorithm in bytes.
26 const BlockSize = 64
27
28 const (
29         _s0 = 0x67452301
30         _s1 = 0xefcdab89
31         _s2 = 0x98badcfe
32         _s3 = 0x10325476
33         _s4 = 0xc3d2e1f0
34 )
35
36 // digest represents the partial evaluation of a checksum.
37 type digest struct {
38         s  [5]uint32       // running context
39         x  [BlockSize]byte // temporary buffer
40         nx int             // index into x
41         tc uint64          // total count of bytes processed
42 }
43
44 func (d *digest) Reset() {
45         d.s[0], d.s[1], d.s[2], d.s[3], d.s[4] = _s0, _s1, _s2, _s3, _s4
46         d.nx = 0
47         d.tc = 0
48 }
49
50 // New returns a new hash.Hash computing the checksum.
51 func New() hash.Hash {
52         result := new(digest)
53         result.Reset()
54         return result
55 }
56
57 func (d *digest) Size() int { return Size }
58
59 func (d *digest) Write(p []byte) (nn int, err os.Error) {
60         nn = len(p)
61         d.tc += uint64(nn)
62         if d.nx > 0 {
63                 n := len(p)
64                 if n > BlockSize-d.nx {
65                         n = BlockSize - d.nx
66                 }
67                 for i := 0; i < n; i++ {
68                         d.x[d.nx+i] = p[i]
69                 }
70                 d.nx += n
71                 if d.nx == BlockSize {
72                         _Block(d, d.x[0:])
73                         d.nx = 0
74                 }
75                 p = p[n:]
76         }
77         n := _Block(d, p)
78         p = p[n:]
79         if len(p) > 0 {
80                 d.nx = copy(d.x[:], p)
81         }
82         return
83 }
84
85 func (d0 *digest) Sum() []byte {
86         // Make a copy of d0 so that caller can keep writing and summing.
87         d := new(digest)
88         *d = *d0
89
90         // Padding.  Add a 1 bit and 0 bits until 56 bytes mod 64.
91         tc := d.tc
92         var tmp [64]byte
93         tmp[0] = 0x80
94         if tc%64 < 56 {
95                 d.Write(tmp[0 : 56-tc%64])
96         } else {
97                 d.Write(tmp[0 : 64+56-tc%64])
98         }
99
100         // Length in bits.
101         tc <<= 3
102         for i := uint(0); i < 8; i++ {
103                 tmp[i] = byte(tc >> (8 * i))
104         }
105         d.Write(tmp[0:8])
106
107         if d.nx != 0 {
108                 panic("d.nx != 0")
109         }
110
111         p := make([]byte, 20)
112         j := 0
113         for _, s := range d.s {
114                 p[j], p[j+1], p[j+2], p[j+3] = byte(s), byte(s>>8), byte(s>>16), byte(s>>24)
115                 j += 4
116         }
117         return p
118 }