-
Notifications
You must be signed in to change notification settings - Fork 5
/
hash.go
90 lines (80 loc) · 2.15 KB
/
hash.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package krypto
import (
"fmt"
"hash"
"strconv"
)
// Hash identifies a cryptographic hash function that is implemented in another
// package.
type KryptoHash uint
func (h KryptoHash) String() string {
switch h {
case HAS160:
return "HAS-160"
case LSH256_224:
return "LSH-256-224"
case LSH256:
return "LSH-256"
case LSH512_224:
return "LSH-512-224"
case LSH512_256:
return "LSH-512-256"
case LSH512_384:
return "LSH-512-384"
case LSH512:
return "LSH-512"
default:
return "unknown hash value " + strconv.Itoa(int(h))
}
}
const (
_ KryptoHash = iota
HAS160 // import github.com/RyuaNerin/go-krypto/has160
LSH256_224 // import github.com/RyuaNerin/go-krypto/lsh256
LSH256 // import github.com/RyuaNerin/go-krypto/lsh256
LSH512_224 // import github.com/RyuaNerin/go-krypto/lsh512
LSH512_256 // import github.com/RyuaNerin/go-krypto/lsh512
LSH512_384 // import github.com/RyuaNerin/go-krypto/lsh512
LSH512 // import github.com/RyuaNerin/go-krypto/lsh512
maxHash
)
var digestSizes = []uint8{
HAS160: 20,
LSH256_224: 28,
LSH256: 32,
LSH512_224: 28,
LSH512_256: 32,
LSH512_384: 48,
LSH512: 64,
}
func (h KryptoHash) Size() int {
if h > 0 && h < maxHash {
return int(digestSizes[h])
}
panic(msgSizeOfUnknwon)
}
var hashes = make([]func() hash.Hash, maxHash)
// New returns a new hash.Hash calculating the given hash function. New panics
// if the hash function is not linked into the binary.
func (h KryptoHash) New() hash.Hash {
if h > 0 && h < maxHash {
f := hashes[h]
if f != nil {
return f()
}
}
panic(fmt.Sprintf(msgUnavailableFormat, h))
}
// Available reports whether the given hash function is linked into the binary.
func (h KryptoHash) Available() bool {
return h < maxHash && hashes[h] != nil
}
// RegisterHash registers a function that returns a new instance of the given
// hash function. This is intended to be called from the init function in
// packages that implement hash functions.
func RegisterHash(h KryptoHash, f func() hash.Hash) {
if h >= maxHash {
panic(msgRegisterHashOfUnknown)
}
hashes[h] = f
}