tshacontext.nim 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # bug #14136
  2. type
  3. MDigest*[bits: static[int]] = object
  4. ## Message digest type
  5. data*: array[bits div 8, byte]
  6. Sha2Context*[bits: static[int],
  7. bsize: static[int],
  8. T: uint32|uint64] = object
  9. count: array[2, T]
  10. state: array[8, T]
  11. buffer: array[bsize, byte]
  12. sha256* = Sha2Context[256, 64, uint32]
  13. template hmacSizeBlock*(h: typedesc): int =
  14. when (h is Sha2Context):
  15. int(h.bsize)
  16. else:
  17. {.fatal: "Choosen hash primitive is not yet supported!".}
  18. type
  19. HMAC*[HashType] = object
  20. ## HMAC context object.
  21. mdctx: HashType
  22. opadctx: HashType
  23. ipad: array[HashType.hmacSizeBlock, byte]
  24. opad: array[HashType.hmacSizeBlock, byte]
  25. func hkdfExtract*[T;S,I: char|byte](ctx: var HMAC[T],
  26. prk: var MDigest[T.bits], # <------- error here "Error: type expected"
  27. salt: openArray[S],
  28. ikm: openArray[I]
  29. ) =
  30. discard
  31. var ctx: HMAC[sha256]
  32. var prk: MDigest[sha256.bits]
  33. let salt = [byte 0x00, 0x01, 0x02]
  34. let ikm = "CompletelyRandomInput"
  35. ctx.hkdfExtract(prk, salt, ikm)