https://toitlang.org/ logo
This is what I have so far:
# general
i
This is what I have so far:
Copy code
import crypto.hmac

class HKDF: 

  static hkdf_extract_ salt/ByteArray ikm/ByteArray -> ByteArray:
    return  hmac.hmac-sha256 --key=salt ikm 

  static hkdf_expand_ prk/ByteArray info/ByteArray length/int -> ByteArray:
    hash_len := 32  
    n := (length + hash_len - 1) / hash_len
    okm := #[]
    previous := #[]
    
    for i := 1; i <= n; i++:       
      data := previous + info + #[i]
      previous =  hmac.hmac-sha256 --key=prk data
      okm += previous

    return okm[0..length]

  static derive --salt/ByteArray  --ikm/ByteArray --info/ByteArray --length/int -> ByteArray:
    prk := hkdf_extract_ salt ikm
    return hkdf_expand_ prk info length
f
Minor comment. I would name the class
Hkdf
.
The
data
and
okm
are using byte-array
+
operations. It could be interesting to use an
io.Buffer
instead (or if the size is know in the beginning just a
ByteBuffer
with
replace
calls.) But that's just optimizations.
If the class doesn't have any fields or member methods, you could also consider just having top-level functions. If the code is in a package, then it usually has the package's name's prefix anyway.
and again: thanks!
i
Thank you for the corrections and for teaching us a little more each day about what you have built for everyone.
5 Views