Skip to content

Instantly share code, notes, and snippets.

@jstn
Last active January 24, 2022 15:33
Show Gist options
  • Save jstn/787da74ab4be9d4cf3cb to your computer and use it in GitHub Desktop.
Save jstn/787da74ab4be9d4cf3cb to your computer and use it in GitHub Desktop.

Revisions

  1. jstn revised this gist Sep 6, 2016. 2 changed files with 3 additions and 1 deletion.
    1 change: 0 additions & 1 deletion Bridging-Header.h
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    #import <CommonCrypto/CommonCrypto.h>
    3 changes: 3 additions & 0 deletions MD5.swift
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    // requires a bridging header with this:
    // #import <CommonCrypto/CommonCrypto.h>

    func MD5(_ string: String) -> String? {
    let length = Int(CC_MD5_DIGEST_LENGTH)
    var digest = [UInt8](repeating: 0, count: length)
  2. jstn revised this gist Sep 6, 2016. No changes.
  3. jstn revised this gist Sep 6, 2016. 2 changed files with 11 additions and 13 deletions.
    1 change: 1 addition & 0 deletions Bridging-Header.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    #import <CommonCrypto/CommonCrypto.h>
    23 changes: 10 additions & 13 deletions MD5.swift
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,12 @@
    func MD5(string: String) -> String {
    let data = (string as NSString).dataUsingEncoding(NSUTF8StringEncoding)
    let result = NSMutableData(length: Int(CC_MD5_DIGEST_LENGTH))
    let resultBytes = UnsafeMutablePointer<CUnsignedChar>(result.bytes)
    CC_MD5(data.bytes, CC_LONG(data.length), resultBytes)

    let a = UnsafeBufferPointer<CUnsignedChar>(start: resultBytes, length: result.length)
    let hash = NSMutableString()

    for i in a {
    hash.appendFormat("%02x", i)
    func MD5(_ string: String) -> String? {
    let length = Int(CC_MD5_DIGEST_LENGTH)
    var digest = [UInt8](repeating: 0, count: length)
    if let d = string.data(using: String.Encoding.utf8) {
    d.withUnsafeBytes { (body: UnsafePointer<UInt8>) in
    CC_MD5(body, CC_LONG(d.count), &digest)
    }
    }
    return (0..<length).reduce("") {
    $0 + String(format: "%02x", digest[$1])
    }

    return hash
    }
  4. jstn revised this gist Aug 5, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions MD5.swift
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,7 @@ func MD5(string: String) -> String {

    let a = UnsafeBufferPointer<CUnsignedChar>(start: resultBytes, length: result.length)
    let hash = NSMutableString()

    for i in a {
    hash.appendFormat("%02x", i)
    }
  5. jstn revised this gist Aug 5, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion MD5.swift
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ func MD5(string: String) -> String {
    let a = UnsafeBufferPointer<CUnsignedChar>(start: resultBytes, length: result.length)
    let hash = NSMutableString()
    for i in a {
    hash.appendFormat("%02x", a[Int(i)])
    hash.appendFormat("%02x", i)
    }

    return hash
  6. jstn revised this gist Aug 4, 2014. 1 changed file with 3 additions and 5 deletions.
    8 changes: 3 additions & 5 deletions MD5.swift
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,11 @@
    func MD5(string: String) -> String {
    let data = string.bridgeToObjectiveC().dataUsingEncoding(NSUTF8StringEncoding)
    let data = (string as NSString).dataUsingEncoding(NSUTF8StringEncoding)
    let result = NSMutableData(length: Int(CC_MD5_DIGEST_LENGTH))
    let resultBytes = UnsafePointer<CUnsignedChar>(result.bytes)

    let resultBytes = UnsafeMutablePointer<CUnsignedChar>(result.bytes)
    CC_MD5(data.bytes, CC_LONG(data.length), resultBytes)

    let a = UnsafeArray<CUnsignedChar>(start: resultBytes, length: result.length)
    let a = UnsafeBufferPointer<CUnsignedChar>(start: resultBytes, length: result.length)
    let hash = NSMutableString()

    for i in a {
    hash.appendFormat("%02x", a[Int(i)])
    }
  7. jstn revised this gist Aug 3, 2014. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions MD5.swift
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,15 @@
    func MD5(string: String) -> String {
    let data = string.bridgeToObjectiveC().dataUsingEncoding(NSUTF8StringEncoding)
    var result = NSMutableData(length: Int(CC_MD5_DIGEST_LENGTH))
    let bytes = UnsafePointer<CUnsignedChar>(result.mutableBytes)
    let result = NSMutableData(length: Int(CC_MD5_DIGEST_LENGTH))
    let resultBytes = UnsafePointer<CUnsignedChar>(result.bytes)

    CC_MD5(data.bytes, CC_LONG(data.length), bytes)
    CC_MD5(data.bytes, CC_LONG(data.length), resultBytes)

    let a = UnsafeArray<CUnsignedChar>(start: resultBytes, length: result.length)
    let hash = NSMutableString()
    let a = UnsafeArray<CUnsignedChar>(start: bytes, length: result.length)

    for i in 0..<Int(CC_MD5_DIGEST_LENGTH) {
    hash.appendFormat("%02x", a[i])
    for i in a {
    hash.appendFormat("%02x", a[Int(i)])
    }

    return hash
  8. jstn revised this gist Aug 3, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion MD5.swift
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    func MD5(string: String) -> String {
    let data = string.bridgeToObjectiveC().dataUsingEncoding(NSUTF8StringEncoding)
    let result = NSMutableData(length: Int(CC_MD5_DIGEST_LENGTH))
    var result = NSMutableData(length: Int(CC_MD5_DIGEST_LENGTH))
    let bytes = UnsafePointer<CUnsignedChar>(result.mutableBytes)

    CC_MD5(data.bytes, CC_LONG(data.length), bytes)
  9. jstn revised this gist Aug 3, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion MD5.swift
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    func MD5(string: String) -> String {
    let data = string.bridgeToObjectiveC().dataUsingEncoding(NSUTF8StringEncoding)
    let result = NSMutableData(length: Int(CC_MD5_DIGEST_LENGTH))
    let bytes = UnsafePointer<CUnsignedChar>(result.bytes)
    let bytes = UnsafePointer<CUnsignedChar>(result.mutableBytes)

    CC_MD5(data.bytes, CC_LONG(data.length), bytes)

  10. jstn created this gist Aug 3, 2014.
    16 changes: 16 additions & 0 deletions MD5.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    func MD5(string: String) -> String {
    let data = string.bridgeToObjectiveC().dataUsingEncoding(NSUTF8StringEncoding)
    let result = NSMutableData(length: Int(CC_MD5_DIGEST_LENGTH))
    let bytes = UnsafePointer<CUnsignedChar>(result.bytes)

    CC_MD5(data.bytes, CC_LONG(data.length), bytes)

    let hash = NSMutableString()
    let a = UnsafeArray<CUnsignedChar>(start: bytes, length: result.length)

    for i in 0..<Int(CC_MD5_DIGEST_LENGTH) {
    hash.appendFormat("%02x", a[i])
    }

    return hash
    }