Skip to content

Instantly share code, notes, and snippets.

@wszdwp wszdwp/md5Hash.swift
Last active Apr 3, 2018

What would you like to do?
MD5 Hash in Swift(import #import <CommonCrypto/CommonCrypto.h> in yourapplication_Bridging_Header_h)
// Need to create Bridging_Header file and import <CommonCrypto/CommonCrypto.h>
// How to create YourApplication_Briging_Header file?
// Ref:
func md5(inputString: String) -> String! {
let str = inputString.cStringUsingEncoding(NSUTF8StringEncoding)
let strLen = CC_LONG(inputString.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))
let digestLen = Int(CC_MD5_DIGEST_LENGTH)
let result = UnsafeMutablePointer<CUnsignedChar>.alloc(digestLen)
CC_MD5(str!, strLen, result)
var hash = NSMutableString()
for i in 0..<digestLen {
hash.appendFormat("%02x", result[i])
return String(format: hash)

This comment has been minimized.

Copy link

commented Apr 3, 2018

When i was trying to use your function in my Custom Swift Framework, i get these errors. Would you like to tell me how to get ride of them.
Use of unresolved identifier 'CC_LONG'
Use of unresolved identifier 'CC_MD5_DIGEST_LENGTH'
Use of unresolved identifier 'CC_MD5'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.