std.crypto.sha2.Sha256
class pub Sha256
A SHA256 hasher.
Static methods
hash
Show source codeHide source code
fn pub static hash(bytes: ref ByteArray) -> Hash {
let hasher = new
hasher.write(bytes)
hasher.finish
}
fn pub static hash(bytes: ref ByteArray) -> Hash
Generates a hash for the given bytes.
Examples
import std.crypto.sha2 (Sha256)
Sha256.hash('hello')
new
Show source codeHide source code
fn pub static new -> Sha256 {
Sha256(
block: Block.new(SHA256_BLOCK_SIZE),
words: Array.filled(with: 0, times: 64),
size: 0,
a: 0x6A09E667,
b: 0xBB67AE85,
c: 0x3C6EF372,
d: 0xA54FF53A,
e: 0x510E527F,
f: 0x9B05688C,
g: 0x1F83D9AB,
h: 0x5BE0CD19,
)
}
fn pub static new -> Sha256
Returns a new instance of the hasher.
Instance methods
finish
Show source codeHide source code
fn pub move finish -> Hash {
@block.add_padding(8, fn { compress })
@block.write_size_be(@size * 8, at: 56)
compress
let out = ByteArray.filled(with: 0, times: 32)
big.write_i32(@a, into: out, at: 0)
big.write_i32(@b, into: out, at: 4)
big.write_i32(@c, into: out, at: 8)
big.write_i32(@d, into: out, at: 12)
big.write_i32(@e, into: out, at: 16)
big.write_i32(@f, into: out, at: 20)
big.write_i32(@g, into: out, at: 24)
big.write_i32(@h, into: out, at: 28)
Hash.new(out)
}
fn pub move finish -> Hash
Generate a hash based on the current state.
write
Show source codeHide source code
fn pub mut write(bytes: ref ByteArray) {
@size += bytes.size
@block.write_bytes(bytes, fn { compress })
}
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
Hasher
impl Hasher for Sha256