IOS 密码加密 SHA256+GTMBase64
2015-08-04 15:51
696 查看
在项目中,Android系统先完成开发,所以IOS系统就参照Android的完成。在密码加密环节,就必须要保持一致了。
Android代码
IOS方法
Android代码
[code] public class Crypto { /** * Define a hash type enumeration for strong-typing */ public enum HashType { MD5("MD5"), SHA1("SHA-1"), SHA256("SHA-256"), SHA512("SHA-512"); private String algorithm; HashType(String algorithm) { this.algorithm = algorithm; } @Override public String toString() { return this.algorithm; } } /** * Set-up MD5 as the default hashing algorithm */ private static final HashType DEFAULT_HASH_TYPE = HashType.SHA256; /** * Create a password hash using the default hashing algorithm * * @param input * The password * @return The password hash */ public static String passwordHash(String input) { return passwordHash(input, DEFAULT_HASH_TYPE); } public static String passwordHash(String input, HashType hashType) { try { MessageDigest m = MessageDigest.getInstance(hashType.toString()); byte[] out = m.digest(input.getBytes()); return new String(Base64.encodeBase64(out)); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } } }
IOS方法
[code]+ (NSString*) sha256:(NSString *)stringpass { const char *cstr = [stringpass UTF8String]; NSData *data = [NSData dataWithBytes:cstr length:stringpass.length]; uint8_t digest[CC_SHA256_DIGEST_LENGTH]; CC_SHA256(data.bytes,data.length, digest); NSData *da=[[NSData alloc]initWithBytes:digest length:CC_SHA256_DIGEST_LENGTH]; NSData *str=[GTMBase64 encodeData:da]; NSString *output=[[NSString alloc]initWithData:str encoding:NSUTF8StringEncoding]; return output; }
相关文章推荐
- iOS编程-XCode Debug忽略SIGPIPE信号
- iOS 中KVC、KVO、NSNotification、delegate 总结及区别
- iOS Code Signing 学习笔记
- iOS学习笔记9- iOS 如何使用TestFlight进行Beta测试
- Storyboard全解析
- JSPatch – 动态更新iOS APP
- ios--c DAY_6
- 如何解决ios SIGPIPE 导致的崩溃
- Integration with Parse(IOS)
- ios-企业证书创建
- iOS启动页延时
- IOS开发-指纹识别
- iOS学习笔记8-iOS 实现发送邮件和短信
- Attribute Unavailable: First baseline layout attribute on iOS versions prior to 8.0
- iOS学习笔记7- iOS 添加定制字体到应用程序中
- iOS开源项目 FreeBall
- CoreLocation定位服务
- 视图链中如何确定根视图
- iOS启动图片和icon大小集合
- 依赖注入——让iOS代码更简洁