OLD | NEW |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Package crypto collects common cryptographic constants. | 5 // Package crypto collects common cryptographic constants. |
6 package crypto | 6 package crypto |
7 | 7 |
8 import ( | 8 import ( |
9 "hash" | 9 "hash" |
10 ) | 10 ) |
11 | 11 |
12 // Hash identifies a cryptographic hash function that is implemented in another | 12 // Hash identifies a cryptographic hash function that is implemented in another |
13 // package. | 13 // package. |
14 type Hash uint | 14 type Hash uint |
15 | 15 |
16 const ( | 16 const ( |
17 » MD4 Hash = 1 + iota // in package crypto/md4 | 17 » MD4 Hash = 1 + iota // import code.google.com/p/go.crypto/md4 |
18 » MD5 // in package crypto/md5 | 18 » MD5 // import crypto/md5 |
19 » SHA1 // in package crypto/sha1 | 19 » SHA1 // import crypto/sha1 |
20 » SHA224 // in package crypto/sha256 | 20 » SHA224 // import crypto/sha256 |
21 » SHA256 // in package crypto/sha256 | 21 » SHA256 // import crypto/sha256 |
22 » SHA384 // in package crypto/sha512 | 22 » SHA384 // import crypto/sha512 |
23 » SHA512 // in package crypto/sha512 | 23 » SHA512 // import crypto/sha512 |
24 MD5SHA1 // no implementation; MD5+SHA1 used for TLS RS
A | 24 MD5SHA1 // no implementation; MD5+SHA1 used for TLS RS
A |
25 » RIPEMD160 // in package crypto/ripemd160 | 25 » RIPEMD160 // import code.google.com/p/go.crypto/ripemd16
0 |
26 maxHash | 26 maxHash |
27 ) | 27 ) |
28 | 28 |
29 var digestSizes = []uint8{ | 29 var digestSizes = []uint8{ |
30 MD4: 16, | 30 MD4: 16, |
31 MD5: 16, | 31 MD5: 16, |
32 SHA1: 20, | 32 SHA1: 20, |
33 SHA224: 28, | 33 SHA224: 28, |
34 SHA256: 32, | 34 SHA256: 32, |
35 SHA384: 48, | 35 SHA384: 48, |
36 SHA512: 64, | 36 SHA512: 64, |
37 MD5SHA1: 36, | 37 MD5SHA1: 36, |
38 RIPEMD160: 20, | 38 RIPEMD160: 20, |
39 } | 39 } |
40 | 40 |
41 // Size returns the length, in bytes, of a digest resulting from the given hash | 41 // Size returns the length, in bytes, of a digest resulting from the given hash |
42 // function. It doesn't require that the hash function in question be linked | 42 // function. It doesn't require that the hash function in question be linked |
43 // into the program. | 43 // into the program. |
44 func (h Hash) Size() int { | 44 func (h Hash) Size() int { |
45 if h > 0 && h < maxHash { | 45 if h > 0 && h < maxHash { |
46 return int(digestSizes[h]) | 46 return int(digestSizes[h]) |
47 } | 47 } |
48 panic("crypto: Size of unknown hash function") | 48 panic("crypto: Size of unknown hash function") |
49 } | 49 } |
50 | 50 |
51 var hashes = make([]func() hash.Hash, maxHash) | 51 var hashes = make([]func() hash.Hash, maxHash) |
52 | 52 |
53 // New returns a new hash.Hash calculating the given hash function. If the | 53 // New returns a new hash.Hash calculating the given hash function. New panics |
54 // hash function is not linked into the binary, New returns nil. | 54 // if the hash function is not linked into the binary. |
55 func (h Hash) New() hash.Hash { | 55 func (h Hash) New() hash.Hash { |
56 if h > 0 && h < maxHash { | 56 if h > 0 && h < maxHash { |
57 f := hashes[h] | 57 f := hashes[h] |
58 if f != nil { | 58 if f != nil { |
59 return f() | 59 return f() |
60 } | 60 } |
61 } | 61 } |
62 » return nil | 62 » panic("crypto: requested hash function is unavailable") |
| 63 } |
| 64 |
| 65 // Available reports whether the given hash function is linked into the binary. |
| 66 func (h Hash) Available() bool { |
| 67 » return h < maxHash && hashes[h] != nil |
63 } | 68 } |
64 | 69 |
65 // RegisterHash registers a function that returns a new instance of the given | 70 // RegisterHash registers a function that returns a new instance of the given |
66 // hash function. This is intended to be called from the init function in | 71 // hash function. This is intended to be called from the init function in |
67 // packages that implement hash functions. | 72 // packages that implement hash functions. |
68 func RegisterHash(h Hash, f func() hash.Hash) { | 73 func RegisterHash(h Hash, f func() hash.Hash) { |
69 if h >= maxHash { | 74 if h >= maxHash { |
70 panic("crypto: RegisterHash of unknown hash function") | 75 panic("crypto: RegisterHash of unknown hash function") |
71 } | 76 } |
72 hashes[h] = f | 77 hashes[h] = f |
73 } | 78 } |
74 | 79 |
75 // PrivateKey represents a private key using an unspecified algorithm. | 80 // PrivateKey represents a private key using an unspecified algorithm. |
76 type PrivateKey interface{} | 81 type PrivateKey interface{} |
OLD | NEW |