ios - Base64 encoding of CryptoDigest / SHA1 - string doesn't match the result from Java / Python -
i'm trying message digest of string on ios. have tried nv-ios-digest 3rd party hash lib still no use.
below function i'm using base64encoded string of message digest.
-(nsstring*) sha1:(nsstring*)input //sha1- digest { nsdata *data = [input datausingencoding:nsutf8stringencoding]; uint8_t digest[cc_sha1_digest_length]; cc_sha1(data.bytes, data.length, digest); nsmutablestring* output = [nsmutablestring stringwithcapacity:cc_sha1_digest_length * 2]; for(int = 0; < cc_sha1_digest_length; i++){ [output appendformat:@"%02x", digest[i]];//digest } return [nsstring stringwithformat:@"%@",[[[output description] datausingencoding:nsutf8stringencoding]base64encodedstringwithoptions:0]]; //base64 encoded }
here sample input string - '530279591878676249714013992002683ec3a85216db22238a12fcf11a07606ecbfb57b5'
when use string either in java or python same result - '5vnqzrb1jiruieuj0dufgeubuhq='
but in ios 'ztu1mzzhnjuxmdc1mjyyndu0odllntizzdazyjlmodflntfiyjg3na=='
here code i'm using in python:
import hashlib import base64 def checkfordigestkey(somestring): msgdigest = hashlib.sha1() msgdigest.update(somestring) print base64.b64encode(msgdigest.digest())
let me know if there anyway same result ios.
you producing binary digest in python, hexadecimal digest in ios.
the digests otherwise equal:
>>> # ios-produced base64 value ... >>> 'ztu1mzzhnjuxmdc1mjyyndu0odllntizzdazyjlmodflntfiyjg3na=='.decode('base64') 'e5536a65107526245489e523d03b9f81e51bb874' >>> # python-produced base64 value ... >>> '5vnqzrb1jiruieuj0dufgeubuhq='.decode('base64') '\xe5sje\x10u&$t\x89\xe5#\xd0;\x9f\x81\xe5\x1b\xb8t' >>> binascii import hexlify >>> # python-produced value converted hex representation ... >>> hexlify('5vnqzrb1jiruieuj0dufgeubuhq='.decode('base64')) 'e5536a65107526245489e523d03b9f81e51bb874'
use base64.b64encode(msgdigest.hexdigest())
in python produce same value, or base-64 encode digest
bytes instead of hexadecimal characters in ios.
Comments
Post a Comment