首页 > 代码库 > Erlang中的RSA签名
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-----\nXXXRSA_PUBLIC_KEYXXX\n-----END PUBLIC KEY-----">>). -define(RSA_PRIVATE_KEY, <<"-----BEGIN RSA PRIVATE KEY-----\nXXXRSA_PRIVATE_KEYXXX\n-----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‘).
Erlang中的RSA签名
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。