Authenticator.swift 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //
  2. // MAC.swift
  3. // CryptoSwift
  4. //
  5. // Created by Marcin Krzyzanowski on 03/09/14.
  6. // Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
  7. //
  8. /**
  9. * Message Authentication
  10. */
  11. public enum Authenticator {
  12. public enum Error: ErrorProtocol {
  13. case authenticateError
  14. }
  15. /**
  16. Poly1305
  17. - parameter key: 256-bit key
  18. */
  19. case Poly1305(key: Array<UInt8>)
  20. case HMAC(key: Array<UInt8>, variant:CryptoSwift.HMAC.Variant)
  21. /**
  22. Generates an authenticator for message using a one-time key and returns the 16-byte result
  23. - returns: 16-byte message authentication code
  24. */
  25. public func authenticate(_ bytes: Array<UInt8>) throws -> Array<UInt8> {
  26. switch (self) {
  27. case .Poly1305(let key):
  28. guard let auth = CryptoSwift.Poly1305(key: key)?.authenticate(bytes) else {
  29. throw Error.authenticateError
  30. }
  31. return auth
  32. case .HMAC(let key, let variant):
  33. guard let auth = CryptoSwift.HMAC(key: key, variant: variant)?.authenticate(bytes) else {
  34. throw Error.authenticateError
  35. }
  36. return auth
  37. }
  38. }
  39. }