HMAC.swift 3.4 KB

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