md5 hash returns with encapsulation in swift -


i have used md5 , hashed result. however, return this: <8f833933 03a151ea 33bf6e3e bbc28594>. new swift not entirely sure whether it's encapsulation. how rid of less , greater sign?

as can see signature, function returns nsdata object:

func md5(#string: string) -> nsdata 

if want convert string, you’ll need encode in human-readable format. common hexadecimal representation.

if use newer swift 2 function question, returns uint8 array

func md5(string string: string) -> [uint8] {     var digest = [uint8](count: int(cc_md5_digest_length), repeatedvalue: 0)     if let data = string.datausingencoding(nsutf8stringencoding) {         cc_md5(data.bytes, cc_long(data.length), &digest)     }      return digest } 

you use handy tohex function:

func tohex(bytes: [uint8]) -> string {     var numbers = [string]()     byte in bytes {         if byte < 0x10 {             numbers.append("0")         }         numbers.append(string(byte, radix: 16, uppercase: true))     }     return numbers.joinwithseparator("") } 

then returns string:

tohex(md5("hello")) 

(in opinion should prefer pure swift types (array of uint8) on foundation types (nsdata).)


Comments