From fb493fc70466d44c5d15e74c34dc223d7f625547 Mon Sep 17 00:00:00 2001 From: Jacques Grove Date: Sat, 7 Dec 2019 18:20:19 -0800 Subject: [PATCH] Switch the 64 bit hash implementation from 3DES to DES, for null (\0 bytes) keys like we use they are the same because 3DES is then just DES in an encrypt-decrypt-encrypt cycle with a 8 byte \0 key. Signed-off-by: Jacques Grove --- go/vt/vtgate/vindexes/hash.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/go/vt/vtgate/vindexes/hash.go b/go/vt/vtgate/vindexes/hash.go index 43859e4f38a..556e2118cc7 100644 --- a/go/vt/vtgate/vindexes/hash.go +++ b/go/vt/vtgate/vindexes/hash.go @@ -36,8 +36,10 @@ var ( ) // Hash defines vindex that hashes an int64 to a KeyspaceId -// by using null-key 3DES hash. It's Unique, Reversible and +// by using null-key DES hash. It's Unique, Reversible and // Functional. +// Note that at once stage we used a 3DES-based hash here, +// but for a null key as in our case, they are completely equivalent. type Hash struct { name string } @@ -114,11 +116,11 @@ func (vind *Hash) ReverseMap(_ VCursor, ksids [][]byte) ([]sqltypes.Value, error return reverseIds, nil } -var block3DES cipher.Block +var blockDES cipher.Block func init() { var err error - block3DES, err = des.NewTripleDESCipher(make([]byte, 24)) + blockDES, err = des.NewCipher(make([]byte, 8)) if err != nil { panic(err) } @@ -128,7 +130,7 @@ func init() { func vhash(shardKey uint64) []byte { var keybytes, hashed [8]byte binary.BigEndian.PutUint64(keybytes[:], shardKey) - block3DES.Encrypt(hashed[:], keybytes[:]) + blockDES.Encrypt(hashed[:], keybytes[:]) return []byte(hashed[:]) } @@ -137,6 +139,6 @@ func vunhash(k []byte) (uint64, error) { return 0, fmt.Errorf("invalid keyspace id: %v", hex.EncodeToString(k)) } var unhashed [8]byte - block3DES.Decrypt(unhashed[:], k) + blockDES.Decrypt(unhashed[:], k) return binary.BigEndian.Uint64(unhashed[:]), nil }