-
Notifications
You must be signed in to change notification settings - Fork 25
/
register.go
164 lines (142 loc) · 4.14 KB
/
register.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2015-present, Cyrill @ Schumacher.fm and the CoreStore contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hashpool
import (
"crypto/hmac"
"hash"
"sync"
"github.com/corestoreio/errors"
)
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package fnv implements FNV-1 and FNV-1a, non-cryptographic hash functions
// created by Glenn Fowler, Landon Curt Noll, and Phong Vo.
// See
// https://en.wikipedia.org/wiki/Fowler-Noll-Vo_hash_function.
const offset64 hash64 = 14695981039346656037
const prime64 hash64 = 1099511628211
// http://programmers.stackexchange.com/questions/49550/which-hashing-algorithm-is-best-for-uniqueness-and-speed
// maybe switch to siphash if there are too many collisions ...
type hash64 uint64
// fnv64a
func (h hash64) writeBytes(data []byte) hash64 {
if h == 0 {
h = offset64
}
for _, c := range data {
h ^= hash64(c)
h *= prime64
}
return h
}
// fnv64a
func (h hash64) writeStr(data string) hash64 {
if h == 0 {
h = offset64
}
for i := 0; i < len(data); i++ {
h ^= hash64(data[i])
h *= prime64
}
return h
}
// END Copyright 2011 The Go Authors. All rights reserved.
type htVal struct {
hashedName hash64
Tank
hf func() hash.Hash
}
func makeHtVal(name hash64, hh func() hash.Hash) htVal {
return htVal{
hashedName: name,
Tank: New(hh),
hf: hh,
}
}
var db = &struct {
sync.RWMutex
// ht == hash tank
ht map[hash64]htVal
}{
ht: make(map[hash64]htVal),
}
// Register registers a new hash function in the global map. Returns an error
// behaviour of AlreadyExists if you try to register the same name twice. Safe
// for concurrent use.
func Register(name string, hh func() hash.Hash) error {
index := hash64(0).writeStr(name)
db.Lock()
defer db.Unlock()
if _, ok := db.ht[index]; ok {
return errors.AlreadyExists.Newf("[hashpool] %q has already been registered", name)
}
db.ht[index] = makeHtVal(index, hh)
return nil
}
// Deregister removes a previously registered hash and all its HMAC hashes. Safe
// for concurrent use.
func Deregister(name string) {
index := hash64(0).writeStr(name)
db.Lock()
defer db.Unlock()
delete(db.ht, index)
for k, v := range db.ht {
// find all hmac hashes with the same name
if v.hashedName == index {
delete(db.ht, k)
}
}
}
// FromRegistry returns a hash pool for the given name, otherwise a NotFound
// error behaviour. Safe for concurrent use.
func FromRegistry(name string) (Tank, error) {
db.RLock()
defer db.RUnlock()
ht, ok := db.ht[hash64(0).writeStr(name)]
if !ok {
return Tank{}, errors.NotFound.Newf("[hashpool] Unknown Hash %q. Not yet registered?", name)
}
return ht.Tank, nil
}
// MustFromRegistry same as FromRegistry but panics when the name does not
// exists in the internal map. Safe for concurrent use.
func MustFromRegistry(name string) Tank {
t, err := FromRegistry(name)
if err != nil {
panic(err)
}
return t
}
// FromRegisterHMAC returns a pool with a HMAC hash from a previously registered
// hash. Key aka. password. The newly generated hash pool will be internally
// cached with the identifier of name plus key.
func FromRegistryHMAC(name string, key []byte) (Tank, error) {
db.Lock()
defer db.Unlock()
nameIndex := hash64(0).writeStr(name)
hashTnk, ok := db.ht[nameIndex]
if !ok {
return Tank{}, errors.NotFound.Newf("[hashpool] Unknown Hash %q. Not yet registered?", name)
}
index := nameIndex.writeBytes(key)
ht, ok := db.ht[index]
if !ok {
ht = makeHtVal(nameIndex, func() hash.Hash {
return hmac.New(hashTnk.hf, key)
})
db.ht[index] = ht
}
return ht.Tank, nil
}