[Objective-C]MD5 Crpyt(3)实现

本文介绍了Objective-C中如何实现MD5 Crypt(3)算法,提供了测试代码,并给出了在线测试平台链接,参照了FreeBSD的C语言算法。
摘要由CSDN通过智能技术生成

Objective-C代码实现


//
//  CryptMD5.m
//  CryptMD5
//
//  Created by andyliu on 2018/10/12.
//

#import "CryptMD5.h"

#import <CommonCrypto/CommonCrypto.h>

#define CC_MD5_DIGEST_LENGTH 16
#define ROUNDS 1000


static unsigned char itoa64[] =        /* 0 ... 63 => ascii - 64 */
"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

static void to64(char *s, unsigned long v, int n)
{
   
    while (--n >= 0) {
   
        *s++ = itoa64[v & 0x3f];
        v >>= 6;
    }
}

/*
 */
char *md5Crypt(const char *password, unsigned int passwordLength,
               const char *salt, unsigned int saltLength,
               const char *prefix, unsigned int prefixLength)
{
   
    const unsigned int keyLen = passwordLength;
    char *thePrefix = (char *)prefix;
    if (thePrefix == nil) {
   
        thePrefix = "$1$";
    }
    CC_MD5_CTX  md5Context;
    CC_MD5_Init(&md5Context);
    /*
     * The password first, since that is what is most unknown
     */
    CC_MD5_Update(&md5Context, password, keyLen);
    /*
     * Then our magic string
     */
    CC_MD5_Update(&md5Context, thePrefix, prefixLength);
    /*
     * Then the raw salt
     */
    CC_MD5_Update(&md5Context, salt, saltLength);
   
    CC_MD5_CTX  md5Context1;
    CC_MD5_Init(&md5Context1);
    /*
     * Then just as many characters of the MD5(pw,salt,pw)
     */
    CC_MD5_Update(&md5Context1, password, keyLen
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值