forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 6
/
hash.go
128 lines (112 loc) · 2.85 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
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
// Copyright 2014, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package vindexes
import (
"bytes"
"crypto/cipher"
"crypto/des"
"encoding/binary"
"encoding/hex"
"fmt"
"strconv"
"github.com/youtube/vitess/go/sqltypes"
)
// Hash defines vindex that hashes an int64 to a KeyspaceId
// by using null-key 3DES hash. It's Unique, Reversible and
// Functional.
type Hash struct {
name string
}
// NewHash creates a new Hash.
func NewHash(name string, m map[string]string) (Vindex, error) {
return &Hash{name: name}, nil
}
// String returns the name of the vindex.
func (vind *Hash) String() string {
return vind.name
}
// Cost returns the cost of this index as 1.
func (vind *Hash) Cost() int {
return 1
}
// Map returns the corresponding KeyspaceId values for the given ids.
func (vind *Hash) Map(_ VCursor, ids []interface{}) ([][]byte, error) {
out := make([][]byte, 0, len(ids))
for _, id := range ids {
num, err := getNumber(id)
if err != nil {
return nil, fmt.Errorf("hash.Map: %v", err)
}
out = append(out, vhash(num))
}
return out, nil
}
// Verify returns true if id maps to ksid.
func (vind *Hash) Verify(_ VCursor, id interface{}, ksid []byte) (bool, error) {
num, err := getNumber(id)
if err != nil {
return false, fmt.Errorf("hash.Verify: %v", err)
}
return bytes.Compare(vhash(num), ksid) == 0, nil
}
// ReverseMap returns the id from ksid.
func (vind *Hash) ReverseMap(_ VCursor, ksid []byte) (interface{}, error) {
return vunhash(ksid)
}
func getNumber(v interface{}) (int64, error) {
if val, ok := v.([]byte); ok {
v = string(val)
}
if val, ok := v.(sqltypes.Value); ok {
v = val.String()
}
switch v := v.(type) {
case int:
return int64(v), nil
case int32:
return int64(v), nil
case int64:
return v, nil
case uint:
return int64(v), nil
case uint32:
return int64(v), nil
case uint64:
return int64(v), nil
case string:
signed, err := strconv.ParseInt(v, 0, 64)
if err == nil {
return signed, nil
}
unsigned, err := strconv.ParseUint(v, 0, 64)
if err == nil {
return int64(unsigned), nil
}
return 0, fmt.Errorf("getNumber: %v", err)
}
return 0, fmt.Errorf("unexpected type for %v: %T", v, v)
}
var block3DES cipher.Block
func init() {
var err error
block3DES, err = des.NewTripleDESCipher(make([]byte, 24))
if err != nil {
panic(err)
}
Register("hash", NewHash)
}
func vhash(shardKey int64) []byte {
var keybytes, hashed [8]byte
binary.BigEndian.PutUint64(keybytes[:], uint64(shardKey))
block3DES.Encrypt(hashed[:], keybytes[:])
return []byte(hashed[:])
}
func vunhash(k []byte) (int64, error) {
if len(k) != 8 {
return 0, fmt.Errorf("invalid keyspace id: %v", hex.EncodeToString(k))
}
var unhashed [8]byte
block3DES.Decrypt(unhashed[:], k)
return int64(binary.BigEndian.Uint64(unhashed[:])), nil
}