HMAC.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // HMAC.swift
  3. // CryptoSwift
  4. //
  5. // Copyright (C) 2014-2017 Marcin Krzyżanowski <marcin@krzyzanowskim.com>
  6. // This software is provided 'as-is', without any express or implied warranty.
  7. //
  8. // In no event will the authors be held liable for any damages arising from the use of this software.
  9. //
  10. // Permission is granted to anyone to use this software for any purpose,including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
  11. //
  12. // - The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation is required.
  13. // - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  14. // - This notice may not be removed or altered from any source or binary distribution.
  15. //
  16. public final class HMAC: Authenticator {
  17. public enum Error: Swift.Error {
  18. case authenticateError
  19. case invalidInput
  20. }
  21. public enum Variant {
  22. case sha1, sha256, sha384, sha512, md5
  23. var digestLength: Int {
  24. switch self {
  25. case .sha1:
  26. return SHA1.digestLength
  27. case .sha256:
  28. return SHA2.Variant.sha256.digestLength
  29. case .sha384:
  30. return SHA2.Variant.sha384.digestLength
  31. case .sha512:
  32. return SHA2.Variant.sha512.digestLength
  33. case .md5:
  34. return MD5.digestLength
  35. }
  36. }
  37. func calculateHash(_ bytes: Array<UInt8>) -> Array<UInt8>? {
  38. switch self {
  39. case .sha1:
  40. return Digest.sha1(bytes)
  41. case .sha256:
  42. return Digest.sha256(bytes)
  43. case .sha384:
  44. return Digest.sha384(bytes)
  45. case .sha512:
  46. return Digest.sha512(bytes)
  47. case .md5:
  48. return Digest.md5(bytes)
  49. }
  50. }
  51. func blockSize() -> Int {
  52. switch self {
  53. case .md5:
  54. return MD5.blockSize
  55. case .sha1, .sha256:
  56. return 64
  57. case .sha384, .sha512:
  58. return 128
  59. }
  60. }
  61. }
  62. var key: Array<UInt8>
  63. let variant: Variant
  64. public init(key: Array<UInt8>, variant: HMAC.Variant = .md5) {
  65. self.variant = variant
  66. self.key = key
  67. if key.count > variant.blockSize() {
  68. if let hash = variant.calculateHash(key) {
  69. self.key = hash
  70. }
  71. }
  72. if key.count < variant.blockSize() {
  73. self.key = ZeroPadding().add(to: key, blockSize: variant.blockSize())
  74. }
  75. }
  76. // MARK: Authenticator
  77. public func authenticate(_ bytes: Array<UInt8>) throws -> Array<UInt8> {
  78. var opad = Array<UInt8>(repeating: 0x5c, count: variant.blockSize())
  79. for idx in key.indices {
  80. opad[idx] = key[idx] ^ opad[idx]
  81. }
  82. var ipad = Array<UInt8>(repeating: 0x36, count: variant.blockSize())
  83. for idx in key.indices {
  84. ipad[idx] = key[idx] ^ ipad[idx]
  85. }
  86. guard let ipadAndMessageHash = variant.calculateHash(ipad + bytes),
  87. let result = variant.calculateHash(opad + ipadAndMessageHash) else {
  88. throw Error.authenticateError
  89. }
  90. // return Array(result[0..<10]) // 80 bits
  91. return result
  92. }
  93. }