zoukankan      html  css  js  c++  java
  • Erlang中的RSA签名

    RSA签名校验

    -spec check_rsa_sign(DataBin, Sign, RSAPublicKeyBin, DigestType) -> boolean when
        DataBin :: binary(), 
        Sign :: binary(),
        RSAPublicKeyBin :: binary()
        DigestType :: 'md5' | 'sha' | 'sha224' | 'sha256' | 'sha384' | 'sha512'.
    check_rsa_sign(DataBin, Sign, RSAPublicKeyBin, DigestType) ->
        PemEntries = public_key:pem_decode(RSAPublicKeyBin),
        RSAPubKey = public_key:pem_entry_decode(hd(PemEntries)),
        Base64Sign = base64:decode(Sign),
        public_key:verify(DataBin, DigestType, Base64Sign, RSAPubKey).

    产生RSA签名

    -spec gen_rsa_sign(MsgBin, DigestType, KeyBin) -> binary() when
        MsgBin :: binary(),
        DigestType :: 'md5' | 'sha' | 'sha224' | 'sha256' | 'sha384' | 'sha512',
        KeyBin :: binary().
    gen_rsa_sign(MsgBin, DigestType, KeyBin) ->
        [Entry] = public_key:pem_decode(KeyBin),
        RSAPriKey = public_key:pem_entry_decode(Entry),
        SignBin = public_key:sign(MsgBin, DigestType, RSAPriKey),
        base64:encode(SignBin).

    示例

    -define(RSA_PUBLIC_KEY, <<"-----BEGIN PUBLIC KEY-----
    XXXRSA_PUBLIC_KEYXXX
    -----END PUBLIC KEY-----">>).
    -define(RSA_PRIVATE_KEY, <<"-----BEGIN RSA PRIVATE KEY-----
    XXXRSA_PRIVATE_KEYXXX
    -----END RSA PRIVATE KEY-----">>).
    
    test() ->
        DataBin = make_data(),
        gen_rsa_sign(DataBin, 'md5', ?RSA_PRIVATE_KEY),
        check_rsa_sign(DataBin, Sign, ?RSA_PUBLIC_KEY, 'md5').
  • 相关阅读:
    POJ
    归并排序+归并排序求逆序对(例题P1908)
    HDU
    2018-12-5 及 codeforces round 525v2
    2018-12-1学习纪录
    近期总结和未来规划
    C++ storage allocation + Dynamic memory allocation + setting limits + initializer list (1)
    注意项
    第四课 计算机的基本组成
    第二课+第三课 计算机系统概论
  • 原文地址:https://www.cnblogs.com/sunbin-hello/p/6480923.html
Copyright © 2011-2022 走看看