-
Notifications
You must be signed in to change notification settings - Fork 21
/
shorthash.go
48 lines (38 loc) · 1010 Bytes
/
shorthash.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
package sodium
// #cgo pkg-config: libsodium
// #include <stdlib.h>
// #include <sodium.h>
import "C"
var (
cryptoShortHashBytes = int(C.crypto_shorthash_bytes())
cryptoShortHashKeyBytes = int(C.crypto_shorthash_keybytes())
)
type ShortHash struct {
Bytes
}
func (s ShortHash) Size() int {
return cryptoShortHashBytes
}
type ShortHashKey struct {
Bytes
}
func (s ShortHashKey) Size() int {
return cryptoShortHashKeyBytes
}
//Shorthash use a secret key and input to produce a ShortHash.
//It is protective to short input. And it's output is also too short to
//be collision-resistent, however it can be used in hash table, Bloom filter
//or generate MAC for interactive protocol.
func (b Bytes) Shorthash(key ShortHashKey) (out Bytes) {
checkTypedSize(&key, "key")
bp, bl := plen(b)
out = make([]byte, cryptoShortHashBytes)
if int(C.crypto_shorthash(
(*C.uchar)(&out[0]),
(*C.uchar)(bp),
(C.ulonglong)(bl),
(*C.uchar)(&key.Bytes[0]))) != 0 {
panic("see libsodium")
}
return
}