Portable IEEE CRC-32 with streaming, checksum combination, and SIMD processing
///|
test "hash and stream" {
assert_eq(@crc32.hash(b"123456789"), 0xCBF43926U)
assert_eq(@crc32.hash(b"123456789", simd=true), 0xCBF43926U)
let state = @crc32.Hasher::new()
state.update(b"1234")
state.update(b"56789")
assert_eq(state.finalize(), 0xCBF43926U)
}///|
test "combine blocks" {
let joined = @crc32.combine(@crc32.hash(b"hello"), @crc32.hash(b" world"), 6)
assert_eq(joined, @crc32.hash(b"hello world"))
}type Hashertest {
let hasher = @crc32.Hasher::new()
hasher.update(b"1234")
hasher.update(b"56789")
assert_eq(hasher.finalize(), 0xCBF43926U)
assert_eq(hasher.total_len(), 9UL)
}test {
let prefix = @crc32.Hasher::new()
prefix.update(b"hello")
let suffix = @crc32.Hasher::new()
suffix.update(b" world")
prefix.combine(suffix)
assert_eq(prefix.finalize(), @crc32.hash(b"hello world"))
assert_eq(prefix.total_len(), 11UL)
assert_eq(suffix.total_len(), 6UL)
}test {
let prefix = @crc32.Hasher::new()
prefix.update(b"hello")
let branch = prefix.copy()
branch.update(b" world")
assert_eq(prefix.finalize(), @crc32.hash(b"hello"))
assert_eq(branch.finalize(), @crc32.hash(b"hello world"))
}test {
let state = @crc32.Hasher::new_with_initial_len(@crc32.hash(b"hello"), 5)
state.update(b" world")
assert_eq(state.finalize(), @crc32.hash(b"hello world"))
assert_eq(state.total_len(), 11UL)
}fn combine(first : UInt, second : UInt, second_len : UInt64) -> UInttest {
let joined = @crc32.combine(@crc32.hash(b"hello"), @crc32.hash(b" world"), 6)
assert_eq(joined, @crc32.hash(b"hello world"))
}fn hash(data : BytesView, simd? : Bool) -> UInttest {
assert_eq(@crc32.hash(b"123456789"), 0xCBF43926U)
assert_eq(@crc32.hash(b""), 0U)
}fn update(initial : UInt, data : BytesView, simd? : Bool) -> UInttest {
let prefix = @crc32.hash(b"1234")
assert_eq(@crc32.update(prefix, b"56789"), 0xCBF43926U)
}Install
Download zipPortable IEEE CRC-32 with streaming, checksum combination, and SIMD processing