SHA1.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // SHA1.swift
  3. // Swifter
  4. //
  5. // Copyright © 2016 Damian Kołakowski. All rights reserved.
  6. //
  7. #if os(Linux)
  8. import Glibc
  9. #else
  10. import Foundation
  11. #endif
  12. public struct SHA1 {
  13. public static func hash(_ input: [UInt8]) -> [UInt8] {
  14. // Alghorithm from: https://en.wikipedia.org/wiki/SHA-1
  15. var message = input
  16. var h0 = UInt32(littleEndian: 0x67452301)
  17. var h1 = UInt32(littleEndian: 0xEFCDAB89)
  18. var h2 = UInt32(littleEndian: 0x98BADCFE)
  19. var h3 = UInt32(littleEndian: 0x10325476)
  20. var h4 = UInt32(littleEndian: 0xC3D2E1F0)
  21. // ml = message length in bits (always a multiple of the number of bits in a character).
  22. let ml = UInt64(message.count * 8)
  23. // append the bit '1' to the message e.g. by adding 0x80 if message length is a multiple of 8 bits.
  24. message.append(0x80)
  25. // append 0 ≤ k < 512 bits '0', such that the resulting message length in bits is congruent to −64 ≡ 448 (mod 512)
  26. let padBytesCount = ( message.count + 8 ) % 64
  27. message.append(contentsOf: [UInt8](repeating: 0, count: 64 - padBytesCount))
  28. // append ml, in a 64-bit big-endian integer. Thus, the total length is a multiple of 512 bits.
  29. var mlBigEndian = ml.bigEndian
  30. let bytePtr = withUnsafePointer(&mlBigEndian) { UnsafeBufferPointer<UInt8>(start: UnsafePointer($0), count: sizeofValue(mlBigEndian)) }
  31. message.append(contentsOf: Array(bytePtr))
  32. // Process the message in successive 512-bit chunks ( 64 bytes chunks ):
  33. for chunkStart in 0..<message.count/64 {
  34. var words = [UInt32]()
  35. let chunk = message[chunkStart*64..<chunkStart*64+64]
  36. // break chunk into sixteen 32-bit big-endian words w[i], 0 ≤ i ≤ 15
  37. for i in 0...15 {
  38. let value = chunk.withUnsafeBufferPointer({ UnsafePointer<UInt32>($0.baseAddress! + (i*4)).pointee})
  39. words.append(value.bigEndian)
  40. }
  41. // Extend the sixteen 32-bit words into eighty 32-bit words:
  42. for i in 16...79 {
  43. let value: UInt32 = ((words[i-3]) ^ (words[i-8]) ^ (words[i-14]) ^ (words[i-16]))
  44. words.append(rotateLeft(value, 1))
  45. }
  46. // Initialize hash value for this chunk:
  47. var a = h0
  48. var b = h1
  49. var c = h2
  50. var d = h3
  51. var e = h4
  52. for i in 0..<80 {
  53. var f = UInt32(0)
  54. var k = UInt32(0)
  55. switch i {
  56. case 0...19:
  57. f = (b & c) | ((~b) & d)
  58. k = 0x5A827999
  59. case 20...39:
  60. f = b ^ c ^ d
  61. k = 0x6ED9EBA1
  62. case 40...59:
  63. f = (b & c) | (b & d) | (c & d)
  64. k = 0x8F1BBCDC
  65. case 60...79:
  66. f = b ^ c ^ d
  67. k = 0xCA62C1D6
  68. default: break
  69. }
  70. let temp = (rotateLeft(a, 5) &+ f &+ e &+ k &+ words[i]) & 0xFFFFFFFF
  71. e = d
  72. d = c
  73. c = rotateLeft(b, 30)
  74. b = a
  75. a = temp
  76. }
  77. // Add this chunk's hash to result so far:
  78. h0 = ( h0 &+ a ) & 0xFFFFFFFF
  79. h1 = ( h1 &+ b ) & 0xFFFFFFFF
  80. h2 = ( h2 &+ c ) & 0xFFFFFFFF
  81. h3 = ( h3 &+ d ) & 0xFFFFFFFF
  82. h4 = ( h4 &+ e ) & 0xFFFFFFFF
  83. }
  84. // Produce the final hash value (big-endian) as a 160 bit number:
  85. var result = [UInt8]()
  86. let h0Big = h0.bigEndian
  87. let h1Big = h1.bigEndian
  88. let h2Big = h2.bigEndian
  89. let h3Big = h3.bigEndian
  90. let h4Big = h4.bigEndian
  91. result += ([UInt8(h0Big & 0xFF), UInt8((h0Big >> 8) & 0xFF), UInt8((h0Big >> 16) & 0xFF), UInt8((h0Big >> 24) & 0xFF)]);
  92. result += ([UInt8(h1Big & 0xFF), UInt8((h1Big >> 8) & 0xFF), UInt8((h1Big >> 16) & 0xFF), UInt8((h1Big >> 24) & 0xFF)]);
  93. result += ([UInt8(h2Big & 0xFF), UInt8((h2Big >> 8) & 0xFF), UInt8((h2Big >> 16) & 0xFF), UInt8((h2Big >> 24) & 0xFF)]);
  94. result += ([UInt8(h3Big & 0xFF), UInt8((h3Big >> 8) & 0xFF), UInt8((h3Big >> 16) & 0xFF), UInt8((h3Big >> 24) & 0xFF)]);
  95. result += ([UInt8(h4Big & 0xff), UInt8((h4Big >> 8) & 0xFF), UInt8((h4Big >> 16) & 0xFF), UInt8((h4Big >> 24) & 0xFF)]);
  96. return result
  97. }
  98. private static func rotateLeft(_ v: UInt32, _ n: UInt32) -> UInt32 {
  99. return ((v << n) & 0xFFFFFFFF) | (v >> (32 - n))
  100. }
  101. }
  102. extension String {
  103. public func sha1() -> String {
  104. return self.sha1().reduce("") { $0 + String(format: "%02x", $1) }
  105. }
  106. public func sha1() -> [UInt8] {
  107. return SHA1.hash([UInt8](self.utf8))
  108. }
  109. }