SHA1.swift 4.5 KB

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