全部产品
Search
文档中心

原生函数_签名验证接口函数

更新时间: 2019-08-27

平台还添加了对签名验证的接口函数支持,其使用示例如下所示:

  1. function hexstring2byte(byte bp1, byte bp2) public returns (byte) {
  2. byte bp3;
  3. byte bp4;
  4. if(bp1 >= 0x30 && bp1 <= 0x39) {
  5. bp3 = byte(uint8(bp1) - 0x30);
  6. }
  7. if(bp1 >= 0x41 && bp1 <= 0x46) {
  8. bp3 = byte(uint8(bp1) - 0x41 + 10);
  9. }
  10. if(bp1 >= 0x61 && bp1 <= 0x66) {
  11. bp3 = byte(uint8(bp1) - 0x61 + 10);
  12. }
  13. if(bp2 >= 0x30 && bp2 <= 0x39) {
  14. bp4 = byte(uint8(bp2) - 0x30);
  15. }
  16. if(bp2 >= 0x41 && bp2 <= 0x46) {
  17. bp4 = byte(uint8(bp2) - 0x41 + 10);
  18. }
  19. if(bp2 >= 0x61 && bp2 <= 0x66) {
  20. bp4 = byte(uint8(bp2) - 0x61 + 10);
  21. }
  22. return ((bp3<<4) | bp4);
  23. }
  24. function test_verify_sig_rsa() public returns (bool)
  25. {
  26. bytes memory pub2 = new bytes(512);
  27. bytes memory sig2 = new bytes(256);
  28. bytes memory hash2 = new bytes(32);
  29. bytes memory spub = new bytes(512*2);
  30. bytes memory ssig = new bytes(256*2);
  31. bytes memory shash = new bytes(32*2);
  32. shash = "d7ad397f6ffa5d4f7f11e7217f241607dc30618c236d2c09c1b9ea8fdadee2e8";
  33. spub = "b0a9703c3b5ad632a042abe4fa56d76a7b30a0c060ea2d3476e4b75a4de9ee77df6538bb5d8c65b20d6d841f8f602112135a4e471b820cacc3ea44ecf17d848cd03775f436599c6b5b7414af57d755c6d3a1dbd65a1a12f50a5caf1777133cd0072d1e692f32250cd9db5d24fd6beba5e5c5b5e5d27fcf0953a0a2b8937350551db5b7b40dfa54fb9873e49e391bdd7b09745663edd23bc0dbc2541c45ad205c14c3dc8beb5a4a27bfbbebe79bd2478d2c6a08035b803e6eb0543829d6477bffbb1014c6171b7728b23f0325bf17b5bd458c2ed403767300e862230a22a1c5792d672b639b546c0eef9dd021c3622238d5391c5251b5d7cddb77b460c221bb0900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010001";
  34. ssig = "ab4cba6dd4ee226d4b4a29458a1bad712e728b4b81e83f5d499ac1d8cc62b410b0a8207e266f83f94b54b3f9f2aa986709787a84f326602e3ad288c80bee1d731cd68c3336db607a686491c575add0b0e0315f5895eb9bf76529d30f06e1323b7c60723c1e6c03e53dbbe13db05aa72eece3eac29a7d4c2e6518bcaf7f7e9bc82f719446c88d0b59ee03a28ea61de2f49b916091adaffe73455109e56f742d68e3f880bb645502216c51adfe457bfe00d2d7b91b4e9cdaf6b71c88cbb620349c21c93e65e7230cbcfdd406da01921e5d744bf1694c40cf0f0dc41de42bdb35bc654547c9cf38de829a36836c31b3d2bfa1030cceada0bad681963b9c0843a579";
  35. for (uint ip = 0; ip < 512; ip++) {
  36. pub2[ip] = hexstring2byte(spub[2*ip], spub[2*ip+1]);
  37. }
  38. for ( ip = 0; ip < 256; ip++) {
  39. sig2[ip] = hexstring2byte(ssig[2*ip], ssig[2*ip+1]);
  40. }
  41. for ( ip = 0; ip < 32; ip++) {
  42. hash2[ip] = hexstring2byte(shash[2*ip], shash[2*ip+1]);
  43. }
  44. return verify_sig_rsa(hash2, pub2, sig2);
  45. }

verify_sig_rsa

verify_sig_rsa 为签名校验函数。

函数原型

  1. verify_sig_rsa(bytes hash, bytes pub, bytes sig) returns(bool result);

请求参数

参数 必选 类型 说明
hash bytes 消息数据,原始的消息。如果是 ASCII 字符串,需要转换为 bytes。
pub bytes 公钥数据,用户的公钥,要求编码为 X509 DER。如果原公钥为 PEM 格式,需要先进行 Base64 解码。
sig bytes 签名数据,原始消息的签名,要求编码为 PKCS#1 DER。如果是 PEM 格式,需要先进行 Base64 解码。

返回值

参数 必选 类型 说明
result bool 方法返回值,成功为 true,否则为 false。