6.4. kink/BIN_HASHER¶
6.4.1. type bin_hasher¶
`bin_hasher` is, conceptually, an immutable tuple of (Hash_algorithm, Accumulated_bin). When `Tail_bin` is given to `hash` method, it calculates a hash value of (Accumulated_bin + Tail_bin) by `Hash_algorithm`.
There is no method to return `Accumulated_bin` itself. Thus, implementations are allowed to hold the compressed state calculated from `Accumulated_bin`, instead of `Accumulated_bin` itself.
Example
:BIN.require_from('kink/')
:BIN_HASHER.require_from('kink/')
:Md5 <- BIN_HASHER.for('MD5')
stdout.print_line(Md5.hash.repr)
# md5(∅)
# => (bin 0xd4 0x1d 0x8c 0xd9 0x8f 0x00 0xb2 0x04 0xe9 0x80 0x09 0x98 0xec 0xf8 0x42 0x7e)
stdout.print_line(Md5.hash(BIN.of(1 2 3)).repr)
# md5(1 2 3)
# => (bin 0x52 0x89 0xdf 0x73 0x7d 0xf5 0x73 0x26 0xfc 0xdd 0x22 0x59 0x7a 0xfb 0x1f 0xac)
:Md5_123 <- Md5 + BIN.of(1 2 3)
stdout.print_line(Md5_123.hash.repr)
# md5(1 2 3)
# => (bin 0x52 0x89 0xdf 0x73 0x7d 0xf5 0x73 0x26 0xfc 0xdd 0x22 0x59 0x7a 0xfb 0x1f 0xac)
stdout.print_line(Md5_123.hash(BIN.of(4 5 6)).repr)
# md5(1 2 3 4 5 6)
# => (bin 0x6a 0xc1 0xe5 0x6b 0xc7 0x8f 0x03 0x10 0x59 0xbe 0x7b 0xe8 0x54 0x52 0x2c 0x4c)
6.4.1.1. Hasher.hash(...[Tail_bin=BIN.of()])¶
`hash` calculates a hash value from (Accumulated_bin + Tail_bin), then return it as a `bin`.
Precondition
`Tail_bin` must be a `bin`
6.4.1.2. Hasher + Bin¶
`+` operator, or `op_add` method, returns a new `bin_hasher` of (Hash_algorithm, Accumulated_bin + Bin).
Precondition
`Bin` must be a `bin`.
6.4.2. BIN_HASHER.for(Hash_algorithm ...[$config={}])¶
`for` makes a `bin_hasher` of (Hash_algorithm, BIN.of()).
Config methods:
• C.on_success($success): default = VAL.identity
• C.on_error($error): default = a fun which raises an exception
If a `bin_hasher` is created, `for` tail-calls $success with the `bin_hasher`.
If `Hash_algorithm` is not supported, `for` tail-calls $error with no arg.
Precondition
`Hash_algorithm` must be a `str`.
$success must be a function which takes a `bin_hasher`.
$error must be a thunk.
6.4.3. BIN_HASHER.is?(Val)¶
`is?` returns whether `Val` is a `bin_hasher`.