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: ErrorType {
  13. case AuthenticateError
  14. }
  15. /**
  16. Poly1305
  17. - parameter key: 256-bit key
  18. */
  19. case Poly1305(key: [UInt8])
  20. case HMAC(key: [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(message: [UInt8]) throws -> [UInt8] {
  26. switch (self) {
  27. case .Poly1305(let key):
  28. guard let auth = CryptoSwift.Poly1305.authenticate(key: key, message: message) else {
  29. throw Error.AuthenticateError
  30. }
  31. return auth
  32. case .HMAC(let key, let variant):
  33. guard let auth = CryptoSwift.HMAC.authenticate(key: key, message: message, variant: variant) else {
  34. throw Error.AuthenticateError
  35. }
  36. return auth
  37. }
  38. }
  39. }