- Notifications
You must be signed in to change notification settings - Fork0
SipHash family of pseudo random functions implemented in Crystal
License
ysbaddaden/siphash.cr
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Crystal implementation of SipHash and HalfSipHash, a family of pseudorandomfunctions optimized for short inputs.
Seehttps://131002.net/siphash/ for more information on the algorithms.
You may choose how many compression-rounds and finalization-rounds to execute.Be wary about your use cases;SipHash(2, 4)
has been verified to becryptographically secure for example, whereasSipHash(1, 3)
is faster but notverified, and should only be used when the result is never disclosed (e.g. fortable hashing).
require"secure_random"require"siphash"key= uninitializedSipHash::KeySecureRandom.random_bytes(key.to_slice)# generate a 64-bit hash:hash=SipHash(2,4).siphash("some data", key)# => UInt64# generate a 128-bit hash:hash=Bytes.new(16)SipHash(2,4).siphash("some data", hash, key)
You may alternatively hash a streaming input as you read it. This implies aslight performance hit, and may only generate 64-bit hashes. This is stilluseful when you don't know the complete input beforehand, or the input isscaterred from different places.
require"secure_random"require"siphash/siphash64"key= uninitializedSipHash64::KeySecureRandom.random_bytes(key.to_slice)hasher=SipHash64(2,4).new(key)hasher.update("some data")hash= hasher.final# => UInt64
An alternativeSipHash
pseudorandom function that uses a 64-bit key andgenerates 32-bit or 64-bit hashes, meant for 32-bit platforms. On 64-bitplatform we advise to useSipHash
instead.
WhileSipHash(2, 4)
has been analyzed and verified to be cryptographicallysecure,HalfSipHash
has not, and isn't expected to be. Results from thehasher should never be disclosed (e.g. use for table hashing on 32-bit).
require"secure_random"require"siphash/halfsiphash"key= uninitializedHalfSipHash::KeySecureRandom.random_bytes(key.to_slice)# generate a 32-bit hash:hash=HalfSipHash(2,4).siphash("some data", key)# => UInt32# generate a 64-bit hash:hash=Bytes.new(8)HalfSipHash(2,4).siphash("some data", hash, key)
A streaming version is also available, limited to 32-bit hashes:
require"secure_random"require"siphash/halfsiphash32"key= uninitializedHalfSipHash32::KeySecureRandom.random_bytes(key.to_slice)hasher=HalfSipHash32(2,4).new(key)hasher.update("some data")hash= hasher.final# => UInt32
Distributed under the Apache 2.0 license.
Created by:
- Jean-Philippe Aumasson
- Daniel J. Bernstein
Ported by:
- Julien Portalier
About
SipHash family of pseudo random functions implemented in Crystal