Search results

There are no results.

std.crypto.hash.Block

type pub Block

A fixed-size block of bytes.

Static methods

new

Show source code
Hide source code
fn pub static new(size: Int) -> Block {
  Block(bytes: ByteArray.filled(with: 0, times: size), index: 0)
}
fn pub static new(size: Int) -> Block

Returns a new Block with the given size in bytes.

Instance methods

add_padding

Show source code
Hide source code
fn pub mut add_padding(size: Int, transform: fn) {
  let pad_to = @bytes.size - size

  if @index >= pad_to {
    @bytes.set(@index := @index + 1, 0x80)

    while @index < @bytes.size { @bytes.set(@index := @index + 1, 0) }

    transform.call
    @index = 0

    while @index < pad_to { @bytes.set(@index := @index + 1, 0) }
  } else {
    @bytes.set(@index := @index + 1, 0x80)

    while @index < pad_to { @bytes.set(@index := @index + 1, 0) }
  }
}
fn pub mut add_padding(size: Int, transform: fn)

Pads the block, calling the supplied closure if the block is full and hashing is necessary.

This method expects the size to be written using a 64-bits integer.

block_index

Show source code
Hide source code
fn pub block_index -> Int {
  @index
}
fn pub block_index -> Int

Returns the current index to write data to.

get

Show source code
Hide source code
fn pub get(index: Int) -> Int {
  @bytes.get(index)
}
fn pub get(index: Int) -> Int

Returns the byte at the given index.

Panics

This method panics if the index is out of bounds.

read_i32_be

Show source code
Hide source code
fn pub read_i32_be(at: Int) -> Int {
  big.read_i32(@bytes, at)
}
fn pub read_i32_be(at: Int) -> Int

Reads an unsigned 32-bits big-endian integer from the given index.

read_i32_le

Show source code
Hide source code
fn pub read_i32_le(at: Int) -> Int {
  little.read_i32(@bytes, at)
}
fn pub read_i32_le(at: Int) -> Int

Reads an unsigned 32-bits little-endian integer from the given index.

read_i64_be

Show source code
Hide source code
fn pub read_i64_be(at: Int) -> Int {
  big.read_i64(@bytes, at)
}
fn pub read_i64_be(at: Int) -> Int

Reads a signed 64-bits big-endian integer from the given index.

set

Show source code
Hide source code
fn pub mut set(index: Int, value: Int) {
  @bytes.set(index, value)
}
fn pub mut set(index: Int, value: Int)

Sets the byte at the given index.

Panics

This method panics if the index is out of bounds.

write_bytes

Show source code
Hide source code
fn pub mut write_bytes(bytes: ref ByteArray, transform: fn) {
  let mut size = bytes.size
  let mut index = 0

  while size > 0 {
    @bytes.set(@index := @index + 1, bytes.get(index))

    if @index == @bytes.size {
      transform.call
      @index = 0
    }

    index += 1
    size -= 1
  }
}
fn pub mut write_bytes(bytes: ref ByteArray, transform: fn)

Writes bytes into the block, calling transform if hashing is necessary.

write_size_be

Show source code
Hide source code
fn pub mut write_size_be(size: Int, at: Int) {
  big.write_i64(size, into: @bytes, at: at)
}
fn pub mut write_size_be(size: Int, at: Int)

Writes a 64-bits big-endian message size into the block.

write_size_le

Show source code
Hide source code
fn pub mut write_size_le(size: Int, at: Int) {
  little.write_i64(size, into: @bytes, at: at)
}
fn pub mut write_size_le(size: Int, at: Int)

Writes a 64-bits little-endian message size into the block.