SHA1.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 SHA1: DigestType {
  16. static let digestLength: Int = 20 // 160 / 8
  17. static let blockSize: Int = 64
  18. fileprivate static let hashInitialValue: ContiguousArray<UInt32> = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0]
  19. fileprivate var accumulated = Array<UInt8>()
  20. fileprivate var processedBytesTotalCount: Int = 0
  21. fileprivate var accumulatedHash: ContiguousArray<UInt32> = SHA1.hashInitialValue
  22. public init() {
  23. }
  24. public func calculate(for bytes: Array<UInt8>) -> Array<UInt8> {
  25. do {
  26. return try update(withBytes: bytes.slice, isLast: true)
  27. } catch {
  28. return []
  29. }
  30. }
  31. fileprivate func process(block chunk: ArraySlice<UInt8>, currentHash hh: inout ContiguousArray<UInt32>) {
  32. // break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15, big-endian
  33. // Extend the sixteen 32-bit words into eighty 32-bit words:
  34. let M = UnsafeMutablePointer<UInt32>.allocate(capacity: 80)
  35. M.initialize(repeating: 0, count: 80)
  36. defer {
  37. M.deinitialize(count: 80)
  38. M.deallocate()
  39. }
  40. for x in 0..<80 {
  41. switch x {
  42. case 0...15:
  43. let start = chunk.startIndex.advanced(by: x * 4) // * MemoryLayout<UInt32>.size
  44. M[x] = UInt32(bytes: chunk, fromIndex: start)
  45. break
  46. default:
  47. M[x] = rotateLeft(M[x - 3] ^ M[x - 8] ^ M[x - 14] ^ M[x - 16], by: 1)
  48. break
  49. }
  50. }
  51. var A = hh[0]
  52. var B = hh[1]
  53. var C = hh[2]
  54. var D = hh[3]
  55. var E = hh[4]
  56. // Main loop
  57. for j in 0...79 {
  58. var f: UInt32 = 0
  59. var k: UInt32 = 0
  60. switch j {
  61. case 0...19:
  62. f = (B & C) | ((~B) & D)
  63. k = 0x5a827999
  64. break
  65. case 20...39:
  66. f = B ^ C ^ D
  67. k = 0x6ed9eba1
  68. break
  69. case 40...59:
  70. f = (B & C) | (B & D) | (C & D)
  71. k = 0x8f1bbcdc
  72. break
  73. case 60...79:
  74. f = B ^ C ^ D
  75. k = 0xca62c1d6
  76. break
  77. default:
  78. break
  79. }
  80. let temp = rotateLeft(A, by: 5) &+ f &+ E &+ M[j] &+ k
  81. E = D
  82. D = C
  83. C = rotateLeft(B, by: 30)
  84. B = A
  85. A = temp
  86. }
  87. hh[0] = hh[0] &+ A
  88. hh[1] = hh[1] &+ B
  89. hh[2] = hh[2] &+ C
  90. hh[3] = hh[3] &+ D
  91. hh[4] = hh[4] &+ E
  92. }
  93. }
  94. extension SHA1: Updatable {
  95. @discardableResult
  96. public func update(withBytes bytes: ArraySlice<UInt8>, isLast: Bool = false) throws -> Array<UInt8> {
  97. accumulated += bytes
  98. if isLast {
  99. let lengthInBits = (processedBytesTotalCount + accumulated.count) * 8
  100. let lengthBytes = lengthInBits.bytes(totalBytes: 64 / 8) // A 64-bit representation of b
  101. // Step 1. Append padding
  102. bitPadding(to: &accumulated, blockSize: SHA1.blockSize, allowance: 64 / 8)
  103. // Step 2. Append Length a 64-bit representation of lengthInBits
  104. accumulated += lengthBytes
  105. }
  106. var processedBytes = 0
  107. for chunk in accumulated.batched(by: SHA1.blockSize) {
  108. if isLast || (accumulated.count - processedBytes) >= SHA1.blockSize {
  109. process(block: chunk, currentHash: &accumulatedHash)
  110. processedBytes += chunk.count
  111. }
  112. }
  113. accumulated.removeFirst(processedBytes)
  114. processedBytesTotalCount += processedBytes
  115. // output current hash
  116. var result = Array<UInt8>(repeating: 0, count: SHA1.digestLength)
  117. var pos = 0
  118. for idx in 0..<accumulatedHash.count {
  119. let h = accumulatedHash[idx].bigEndian
  120. result[pos] = UInt8(h & 0xff)
  121. result[pos + 1] = UInt8((h >> 8) & 0xff)
  122. result[pos + 2] = UInt8((h >> 16) & 0xff)
  123. result[pos + 3] = UInt8((h >> 24) & 0xff)
  124. pos += 4
  125. }
  126. // reset hash value for instance
  127. if isLast {
  128. accumulatedHash = SHA1.hashInitialValue
  129. }
  130. return result
  131. }
  132. }