tcryptodigest.nim 944 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. discard """
  2. output: "Digest[128]\nDigest[256]"
  3. """
  4. import typetraits
  5. type
  6. Digest[bits: static[int]] = object
  7. data: array[bits div 8, byte]
  8. ContextKind = enum
  9. A, B, C
  10. HashingContext[bits: static[int], kind: static[ContextKind]] = object
  11. ctx: array[bits div 8, byte]
  12. Hash128 = HashingContext[128, A]
  13. Hash256 = HashingContext[256, B]
  14. HMAC[HashType] = object
  15. h: HashType
  16. proc init(c: var HashingContext) = discard
  17. proc update(c: var HashingContext, data: ptr byte, dataLen: uint) = discard
  18. proc finish(c: var HashingContext): Digest[c.bits] = discard
  19. proc digest(T: typedesc, data: ptr byte, dataLen: uint): Digest[T.bits] =
  20. mixin init, update, finish
  21. var ctx: T
  22. ctx.init()
  23. ctx.update(data, dataLen)
  24. result = ctx.finish()
  25. var h = Hash128.digest(nil, 0)
  26. echo h.type.name
  27. proc finish(hmac: var HMAC): Digest[HMAC.HashType.bits] =
  28. discard
  29. var hm: HMAC[Hash256]
  30. var d = hm.finish
  31. echo d.type.name