String+SHA1.swift 4.8 KB

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