Last active
March 13, 2018 10:45
-
-
Save jaderfeijo/a42b820288a8a98d99c85e34532ae457 to your computer and use it in GitHub Desktop.
String+MD5.swift
This file contains hidden or 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
| // | |
| // String+MD5.swift | |
| // Created by: Jader Feijo | |
| // | |
| import Foundation | |
| extension String { | |
| var md5: String { | |
| var digest = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH)) | |
| if let data = self.data(using: String.Encoding.utf8) { | |
| CC_MD5((data as NSData).bytes, CC_LONG(data.count), &digest) | |
| } | |
| var digestHex = "" | |
| for index in 0..<Int(CC_MD5_DIGEST_LENGTH) { | |
| digestHex += String(format: "%02x", digest[index]) | |
| } | |
| return digestHex | |
| } | |
| } |
This file contains hidden or 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
| // | |
| // StringMD5Tests.swift | |
| // Created by: Jader Feijo | |
| // | |
| import XCTest | |
| class StringMD5Tests: XCTestCase { | |
| func testMD5() { | |
| XCTAssertEqual("".md5, "d41d8cd98f00b204e9800998ecf8427e") | |
| XCTAssertEqual("test".md5, "098f6bcd4621d373cade4e832627b4f6") | |
| XCTAssertEqual("just to be sure".md5, "9b22815faccfedd0ba143fb8c774335a") | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment