Search results

There are no results.

std.crypto.poly1305.Poly1305

class pub Poly1305

The Poly1305 universal hash function.

Static methods

hash

Show source code
Hide source code
fn pub static hash(key: ref ByteArray, bytes: ref ByteArray) -> Hash {
  let hasher = new(key)

  hasher.write(bytes)
  hasher.finish
}
fn pub static hash(key: ref ByteArray, bytes: ref ByteArray) -> Hash

Hashes a message with the given one-time secret key.

This method is useful if you just want to hash a single message.

Panics

This method panics of the key isn't exactly 32 bytes long.

Examples

import std.rand (Random)
import std.crypto.poly1305 (Poly1305)

let key = Random.new.bytes(size: 32)
let msg = 'This is a test'.to_byte_array

Poly1305.hash(key, msg)

new

Show source code
Hide source code
fn pub static new(key: ref ByteArray) -> Poly1305 {
  if key.size != KEY_SIZE {
    panic('Poly1305 keys must be exactly ${KEY_SIZE} bytes')
  }

  Poly1305(
    block: Block.new(BLOCK_SIZE),
    r0: little.read_u32(from: key, at: 0) & 0x0FFFFFFF,
    r1: little.read_u32(from: key, at: 4) & 0x0FFFFFFC,
    r2: little.read_u32(from: key, at: 8) & 0x0FFFFFFC,
    r3: little.read_u32(from: key, at: 12) & 0x0FFFFFFC,
    pad0: little.read_u32(from: key, at: 16),
    pad1: little.read_u32(from: key, at: 20),
    pad2: little.read_u32(from: key, at: 24),
    pad3: little.read_u32(from: key, at: 28),
    h0: 0,
    h1: 0,
    h2: 0,
    h3: 0,
    h4: 0,
  )
}
fn pub static new(key: ref ByteArray) -> Poly1305

Returns a new Poly1305 hasher.

The key argument is a one-time secret key to use for generating the hash. The key must not be reused for other messages.

Panics

This method panics if the key isn't exactly 32 bytes long.

Instance methods

finish

Show source code
Hide source code
fn pub move finish -> Hash {
  if @block.block_index > 0 {
    let mut i = @block.block_index

    @block.set(i := i + 1, 1)

    while i < BLOCK_SIZE { @block.set(i := i + 1, 0) }

    compress(final: true)
  }

  let mut c = 5

  c = c.wrapping_add(@h0)
  c = c >>> 32
  c = c.wrapping_add(@h1)
  c = c >>> 32
  c = c.wrapping_add(@h2)
  c = c >>> 32
  c = c.wrapping_add(@h3)
  c = c >>> 32
  c = c.wrapping_add(@h4)
  c = c >>> 2 * 5

  let out = ByteArray.filled(with: 0, times: TAG_SIZE)

  c = c.wrapping_add(@h0) + @pad0
  little.write_u32(c, into: out, at: 0)
  c = c >>> 32

  c = c.wrapping_add(@h1) + @pad1
  little.write_u32(c, into: out, at: 4)
  c = c >>> 32

  c = c.wrapping_add(@h2) + @pad2
  little.write_u32(c, into: out, at: 8)
  c = c >>> 32

  c = c.wrapping_add(@h3) + @pad3
  little.write_u32(c, into: out, at: 12)
  c = c >>> 32

  Hash.new(out)
}
fn pub move finish -> Hash

Generate a hash based on the current state.

write

Show source code
Hide source code
fn pub mut write(bytes: ref ByteArray) {
  @block.write_bytes(bytes, fn { compress(final: false) })
}
fn pub mut write(bytes: ref ByteArray)

Writes the bytes into the hasher.

This method is free to modify bytes if needed, so no assumption should be made about its contents after this method returns. If you're reusing the same ByteArray for multiple calls to write, you should clear the ByteArray after each call.

Implemented traits

std.crypto.hash.

Hasher

impl Hasher for Poly1305