-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.go
71 lines (53 loc) · 2.12 KB
/
key.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
// Copyright (c) 2022 Hirotsuna Mizuno. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package filecache
import (
"crypto/sha512"
"encoding/binary"
"encoding/hex"
"fmt"
"strconv"
)
// HashSize is the size, in bytes, of the key hash.
const HashSize = 32
// Hash represents a 32-bytes hash value.
type Hash [HashSize]byte
// Key is the interface implemented by the key type to identify a file cache
// entry.
type Key interface {
fmt.Stringer
Hash() Hash
}
// Uint64Key is a wrapper type to attach the Hash method to uint64.
type Uint64Key uint64
// Hash returns a byte sequence in which the LSBs represent the uint64 value
// itself as a hash value.
func (k Uint64Key) Hash() (b Hash) {
binary.BigEndian.PutUint64(b[HashSize-8:], uint64(k))
return
}
// String returns the string representation of the uint64 value.
func (k Uint64Key) String() string { return strconv.FormatUint(uint64(k), 10) }
// Uint32Key is a wrapper type to attach the Hash method to uint32.
type Uint32Key uint32
// Hash returns a byte sequence in which the LSBs represent the uint32 value
// itself as a hash value.
func (k Uint32Key) Hash() (b Hash) {
binary.BigEndian.PutUint32(b[HashSize-4:], uint32(k))
return
}
// String returns the string representation of the uint32 value.
func (k Uint32Key) String() string { return strconv.FormatUint(uint64(k), 10) }
// StringKey is a wrapper type to attach the Hash method to string.
type StringKey string
// Hash calculates and returns a hash value of the string using SHA-512/256.
func (k StringKey) Hash() Hash { return sha512.Sum512_256([]byte(k)) }
// String returns the string value itself of StringKey.
func (k StringKey) String() string { return string(k) }
// ByteSliceKey is a wrapper type to attach the Hash method to []byte.
type ByteSliceKey []byte
// Hash calculates and returns a hash value of the []byte using SHA-512/256.
func (k ByteSliceKey) Hash() Hash { return sha512.Sum512_256([]byte(k)) }
// String returns the string representation of the []byte key.
func (k ByteSliceKey) String() string { return hex.EncodeToString([]byte(k)) }