当前位置:   article > 正文

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

c++ hmacsha1

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

#include <stdio.h>
#include <string.h>

#include <string>
#include <iostream>

#include <openssl/md5.h>
#include <openssl/hmac.h>
#include <openssl/sha.h>

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

编译执行后,示例打印如下:
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/471933
推荐阅读
相关标签
  

闽ICP备14008679号