Search results

There are no results.

std.hash.siphash.SipHasher13

class pub SipHasher13

An implementation of the SipHash 1-3 algorithm.

Examples

import std.hash.siphash (SipHasher13)
import std.rand (Random)

let rng = Random.new
let hasher = SipHasher13.new(key0: rng.int, key1: rng.int)

hasher.write(42)
hasher.finish

Static methods

default

Show source code
Hide source code
fn pub static default -> SipHasher13 {
  # These casts allow us to read the hash keys from the runtime state, but
  # without having to declare the entire layout (including fields we have no
  # interest in).
  let key0 = (_INKO.state as Int + 16 as Pointer[UInt64]).0 as Int
  let key1 = (_INKO.state as Int + 24 as Pointer[UInt64]).0 as Int

  new(key0, key1)
}
fn pub static default -> SipHasher13

Returns a new hasher using two default keys.

new

Show source code
Hide source code
fn pub static new(key0: Int, key1: Int) -> SipHasher13 {
  SipHasher13(
    size: 0,
    m_index: 0,
    m: 0,
    v0: 0x736F6D6570736575 ^ key0,
    v1: 0x646F72616E646F6D ^ key1,
    v2: 0x6C7967656E657261 ^ key0,
    v3: 0x7465646279746573 ^ key1,
  )
}
fn pub static new(key0: Int, key1: Int) -> SipHasher13

Returns a new hasher using the two keys.

Both keys should be randomly generated. The type std.rand.Random can be used to generate these keys.

Instance methods

finish

Show source code
Hide source code
fn pub move finish -> Int {
  let len = @size

  while @m_index < 7 { write(0) }

  write(len)
  @v2 ^= 0xFF
  round
  round
  round

  @v0 ^ @v1 ^ @v2 ^ @v3
}
fn pub move finish -> Int

Returns the hash for the values written so far.

write

Show source code
Hide source code
fn pub mut write(value: Int) {
  @size += 1
  @m |= (value & 0xFF) << ((@m_index := @m_index + 1) * 8)

  if @m_index < 8 { return }

  @v3 ^= @m
  round
  @v0 ^= @m
  @m_index = 0
  @m = 0
}
fn pub mut write(value: Int)

Writes an Int into the hasher.

Implemented traits

std.hash.

Hasher

impl Hasher for SipHasher13