• 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').
  • 相关阅读:
    一些专有名词词汇
    UE、UI、 IA和IxD傻傻分不清
    关于SNMP4J取值乱码解决方法
    个人推荐
    软件系统分类
    Ext 4.2树节点搜索功能
    android picke ui
    android 滚轮
    一、dialog.show()引起的android.view.WindowManager$BadTokenException错误
    fragment getactivity 为null解决
  • 原文地址:https://www.cnblogs.com/sunbin-hello/p/6480923.html
Copyright © 2020-2023  润新知