HMAC-SHA1加密算法c++与java的实现

HMAC-SHA1是日常用的比较多的一种加密算法,比如与https服务器交互时,数据会使用此方法进行加密,有时候客户端与服务器用的是不同语言实现的,单方面不好验证,故找了多语言的实现。
比如服务器是Apache,
其代码可能是
new HmacUtils(HmacAlgorithms.HMAC_SHA_1, key).hmacHex(data)
后面可能还有其他附加算法,忽略不讨论。
那么对应的客户端多数用的是c++或java
c++实现如下

#include 
#include 

#include 
#include 

#include 
#include 
#include 

std::string HexHmacSha1(const char * key, const char * input)
{
    unsigned char md[20];
    char mdBufs[20*2+1] = {0};
    unsigned int len = 0;

    HMAC(EVP_sha1(), key, strlen(key), (unsigned char*)input, strlen(input), md, &len);

    for (unsigned int i = 0; i < len; ++i)
    {
        sprintf(mdBufs + i*2, "%02x", md[i]);
    }
    printf("in:%s, out:%s\n", input, mdBufs);

    return mdBufs;
}

int main(int argc, char **argv)
{
    const char *key = "key-xxxxx";
    const char *data = "Hello World";
    std::string hexSha = HexHmacSha1(key, data);

    data = "Next Test";
    hexSha = HexHmacSha1(key, data);

    return 0;
}

编译执行后,示例打印如下:
in:Hello World, out:e3729ba295d21330983b64018edfd3affa57a6f2
in:Next Test, out:16978e2117c2f9b727111c8a939cafa4ccd9d751

import java.security.MessageDigest;
import java.util.Formatter;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class CryptoUtil {
    /**
     * MD5加密
     */
    public static String md5s(String plainText) {
        String str = "";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            byte b[] = md.digest();

            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            str = buf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }


    private static String toHexString(byte[] bytes) {
        Formatter formatter = new Formatter();
        for (byte b : bytes) {
            formatter.format("%02x", b);
        }
        return formatter.toString();
    }

    public static String hmacSha1(String value, String key) {
        try {
            byte[] keyBytes = key.getBytes();
            SecretKeySpec signingKey = new SecretKeySpec(keyBytes,"HmacSHA1");
            Mac mac = Mac.getInstance("HmacSHA1");
            mac.init(signingKey);
            byte[] rawHmac = mac.doFinal(value.getBytes());
            return toHexString(rawHmac);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

// 使用示例
String key = "key-xxxxx";
String data = "Hello World";
String hmacHex = CryptoUtil.hmacSha1(data, key);
Log.d("", "in:" + key + "out:" + hmacHex);
// 打印出 in:key-xxxxxout:e3729ba295d21330983b64018edfd3affa57a6f2

两种实现,输出结果一样。
作者:帅得不敢出门

你可能感兴趣的:(C/C++,Android平台,算法,c++,java,开发语言)