Created
June 1, 2015 02:33
-
-
Save hyeonjae/1741c7ec07dfc8742b13 to your computer and use it in GitHub Desktop.
MD5 standalone in Swift 1.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* http://rosettacode.org/wiki/MD5/Implementation#Swift | |
* https://github.com/krzyzanowskim/CryptoSwift/tree/master/CryptoSwift | |
* MD5 - no need to external library version | |
*/ | |
import Foundation | |
let shift : [UInt32] = [7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21] | |
let table: [UInt32] = (0 ..< 64).map { UInt32(0x100000000 * abs(sin(Double($0 + 1)))) } | |
func md5(var message: [UInt8]) -> [UInt8] { | |
var messageLenBits = UInt64(message.count) * 8 | |
message.append(0x80) | |
while message.count % 64 != 56 { | |
message.append(0) | |
} | |
var lengthBytes = [UInt8](count: 8, repeatedValue: 0) | |
UnsafeMutablePointer<UInt64>(lengthBytes).memory = messageLenBits.littleEndian | |
message += lengthBytes | |
var a : UInt32 = 0x67452301 | |
var b : UInt32 = 0xEFCDAB89 | |
var c : UInt32 = 0x98BADCFE | |
var d : UInt32 = 0x10325476 | |
for chunkOffset in stride(from: 0, to: message.count, by: 64) { | |
let chunk = UnsafePointer<UInt32>(UnsafePointer<UInt8>(message) + chunkOffset) | |
let originalA = a | |
let originalB = b | |
let originalC = c | |
let originalD = d | |
for j in 0 ..< 64 { | |
var f : UInt32 = 0 | |
var bufferIndex = j | |
let round = j >> 4 | |
switch round { | |
case 0: | |
f = (b & c) | (~b & d) | |
case 1: | |
f = (b & d) | (c & ~d) | |
bufferIndex = (bufferIndex*5 + 1) & 0x0F | |
case 2: | |
f = b ^ c ^ d | |
bufferIndex = (bufferIndex*3 + 5) & 0x0F | |
case 3: | |
f = c ^ (b | ~d) | |
bufferIndex = (bufferIndex * 7) & 0x0F | |
default: | |
assert(false) | |
} | |
let sa = shift[(round<<2)|(j&3)] | |
let tmp = a &+ f &+ UInt32(littleEndian: chunk[bufferIndex]) &+ table[j] | |
a = d | |
d = c | |
c = b | |
b = b &+ (tmp << sa | tmp >> (32-sa)) | |
} | |
a = a &+ originalA | |
b = b &+ originalB | |
c = c &+ originalC | |
d = d &+ originalD | |
} | |
var result = [UInt8](count: 16, repeatedValue: 0) | |
for (i, n) in enumerate([a, b, c, d]) { | |
UnsafeMutablePointer<UInt32>(result)[i] = n.littleEndian | |
} | |
return result | |
} | |
func toHexString(bytes: [UInt8]) -> String { | |
return "".join(bytes.map { String(format:"%02x", $0) }) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment