简体   繁体   中英

Remove spaces in SHA256 output

Can anyone solve my problem related to SHA256 in iOS?

#import <CommonCrypto/CommonDigest.h>

NSData *dataIn = [@"XXXXX" dataUsingEncoding:NSASCIIStringEncoding];
NSMutableData *macOut = [NSMutableData dataWithLength:CC_SHA256_DIGEST_LENGTH];

CC_SHA256(dataIn.bytes, dataIn.length,  macOut.mutableBytes);

NSLog(@"dataIn: %@", dataIn);
NSLog(@"macOut: %@", macOut);

I getting output like this :

2014-10-14 10:46:43.602 sha256[480:70b] dataIn: <416e616e 64>
2014-10-14 10:46:43.604 sha256[480:70b] macOut: <e9fb6a39 4db5ffd6 dba9b31a c7d9a90d 56f90767 afaf1e14 d6dcaa37 db180932>

what i actually need is i need to remove space in macOut. ie like this

Add this below your code:

NSString *macOutString = [NSString stringWithFormat:@"%@", macOut];
NSString *encryptedString = [macOutString stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"macOut: %@", encryptedString);

You can also verify the encryption with - http://www.xorbin.com/tools/sha256-hash-calculator

创建NSData,NSString类别可在https://github.com/mdznr/iOS-Passcode/blob/master/Passcode/NSString%2Bsha256.m使用此代码

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM