SipHash 算法流程

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

1. souce code and algorithm

Redis SipHash Implementation

SipHash 算法流程_第1张图片

2. 算法流程 hash = SipHash(message, key)

1. 初始化 (Initialization)

1.1 初始化四个向量 v0,v1,v2,v3

uint64_t v0 = 0x736f6d6570736575ULL; //unsigned long long
uint64_t v1 = 0x646f72616e646f6dULL;
uint64_t v2 = 0x6c7967656e657261ULL;
uint64_t v3 = 0x7465646279746573ULL;

1.2 将128位的key用little-endian(较小的字节在低位)编码为64位的k0,k1

// uint8_t key[16]; // key = [8, 7, 6, 5, 4, 3, 2, 1, 8, 7, 6, 5, 4, 3, 2, 1] Byte = total 128-bit
uint64_t k0 = (*(uint64_t*)key); // 64-bit
uint64_t k1 = (*(uint64_t*)(key+8)) // 64-bit

1.3 用k0,k1初始化v0,v1,v2,v3

v3 ^= k1; // xor
v2 ^= k0;
v1 ^= k1;
v0 ^= k0;

2. 压缩 (Compression)

2.1 split message

message = 'abcdefghijk', length = 11
step1: msgs = {'abcdefgh', 'ijk'} // 8 Bytes and 3 Bytes
step2: msgs = {'abcdefgh', '\0\0\0\0\0ijk'} // 8 Bytes and 8 Bytes
step3: msgs = { 0x6162636465666768, 0x0000000000696a6b} // 64-bit and 64-bit

2.2 transfer

for m in msgs
do
    v3 ^= m
    SipRound
    v0 ^= m
done

2.4 SipRound

SipHash 算法流程_第2张图片

3. 结束 (Finalization)

v2 ^= 0xff
SipRound
SipRound
final = v0 ^ v1 ^ v2 ^ v3

转载于:https://my.oschina.net/tigerBin/blog/3038044

你可能感兴趣的:(SipHash 算法流程)