github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/test/test_crypto.js (about) 1 // Copyright (C) 2018 go-nebulas authors 2 // 3 // This file is part of the go-nebulas library. 4 // 5 // the go-nebulas library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // the go-nebulas library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with the go-nebulas library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 19 'use strict'; 20 21 function eq(a, b) { 22 if (a !== b) { 23 throw new Error("Not equal: " + a + " <--> " + b); 24 } 25 } 26 27 var crypto = require('crypto.js'); 28 29 var input = "Nebulas is a next generation public blockchain, aiming for a continuously improving ecosystem." 30 31 // 32 eq(crypto.sha256(input), "a32d6d686968192663b9c9e21e6a3ba1ba9b2e288470c2f98b790256530933e0"); 33 eq(crypto.sha3256(input), "564733f9f3e139b925cfb1e7e50ba8581e9107b13e4213f2e4708d9c284be75b"); 34 eq(crypto.ripemd160(input), "4236aa9974eb7b9ddb0f7a7ed06d4bf3d9c0e386"); 35 eq(crypto.recoverAddress(1, "564733f9f3e139b925cfb1e7e50ba8581e9107b13e4213f2e4708d9c284be75b", "d80e282d165f8c05d8581133df7af3c7c41d51ec7cd8470c18b84a31b9af6a9d1da876ab28a88b0226707744679d4e180691aca6bdef5827622396751a0670c101"), "n1F8QbdnhqpPXDPFT2c9a581tpia8iuF7o2"); 36 eq(crypto.md5(input), "9954125a33a380c3117269cff93f76a7"); 37 eq(crypto.base64(input), "TmVidWxhcyBpcyBhIG5leHQgZ2VuZXJhdGlvbiBwdWJsaWMgYmxvY2tjaGFpbiwgYWltaW5nIGZvciBhIGNvbnRpbnVvdXNseSBpbXByb3ZpbmcgZWNvc3lzdGVtLg=="); 38 39 // alg is not a safe integer 40 try { 41 crypto.recoverAddress(1000000000000000000010000000000000000000, "564733f9f3e139b925cfb1e7e50ba8581e9107b13e4213f2e4708d9c284be75b", 42 "d80e282d165f8c05d8581133df7af3c7c41d51ec7cd8470c18b84a31b9af6a9d1da876ab28a88b0226707744679d4e180691aca6bdef5827622396751a0670c101"); 43 } catch (err) { 44 if (err.message !== "alg must be non-negative integer") { 45 throw err; 46 } 47 } 48 49 // negative alg 50 try { 51 crypto.recoverAddress(-1000, "564733f9f3e139b925cfb1e7e50ba8581e9107b13e4213f2e4708d9c284be75b", 52 "d80e282d165f8c05d8581133df7af3c7c41d51ec7cd8470c18b84a31b9af6a9d1da876ab28a88b0226707744679d4e180691aca6bdef5827622396751a0670c101"); 53 } catch (err) { 54 if (err.message !== "alg must be non-negative integer") { 55 throw err; 56 } 57 } 58 59 // odd hash 60 try { 61 crypto.recoverAddress(1, "564733f9f3e139b925cfb1e7e50ba8581e9107b13e4213f2e4708d9c284be75", 62 "d80e282d165f8c05d8581133df7af3c7c41d51ec7cd8470c18b84a31b9af6a9d1da876ab28a88b0226707744679d4e180691aca6bdef5827622396751a0670c101"); 63 } catch (err) { 64 if (err.message !== "hash & sign must be hex string") { 65 throw err; 66 } 67 } 68 69 // not hex hash 70 try { 71 crypto.recoverAddress(1, "TT564733f9f3e139b925cfb1e7e50ba8581e9107b13e4213f2e4708d9c284be75b", 72 "d80e282d165f8c05d8581133df7af3c7c41d51ec7cd8470c18b84a31b9af6a9d1da876ab28a88b0226707744679d4e180691aca6bdef5827622396751a0670c101"); 73 } catch (err) { 74 if (err.message !== "hash & sign must be hex string") { 75 throw err; 76 } 77 } 78 79 // not hex sign 80 try { 81 crypto.recoverAddress(1, "564733f9f3e139b925cfb1e7e50ba8581e9107b13e4213f2e4708d9c284be75b", 82 "TTd80e282d165f8c05d8581133df7af3c7c41d51ec7cd8470c18b84a31b9af6a9d1da876ab28a88b0226707744679d4e180691aca6bdef5827622396751a0670c101"); 83 } catch (err) { 84 if (err.message !== "hash & sign must be hex string") { 85 throw err; 86 } 87 } 88 89 // odd sign 90 try { 91 crypto.recoverAddress(1, "564733f9f3e139b925cfb1e7e50ba8581e9107b13e4213f2e4708d9c284be75b", 92 "d80e282d165f8c05d8581133df7af3c7c41d51ec7cd8470c18b84a31b9af6a9d1d876ab28a88b0226707744679d4e180691aca6bdef5827622396751a0670c101"); 93 } catch (err) { 94 if (err.message !== "hash & sign must be hex string") { 95 throw err; 96 } 97 } 98 99 try { 100 crypto.nativeCrypto.sha256(1231432); 101 } catch (err) { 102 if (err !== "sha256() requires a string argument") { 103 throw err; 104 } 105 } 106 107 try { 108 crypto.nativeCrypto.sha256(); 109 } catch (err) { 110 if (err !== "sha256() requires only 1 argument") { 111 throw err; 112 } 113 } 114 115 try { 116 crypto.nativeCrypto.sha3256(null); 117 } catch (err) { 118 if (err !== "sha3256() requires a string argument") { 119 throw err; 120 } 121 } 122 123 try { 124 crypto.nativeCrypto.sha3256(); 125 } catch (err) { 126 if (err !== "sha3256() requires only 1 argument") { 127 throw err; 128 } 129 } 130 131 try { 132 crypto.nativeCrypto.ripemd160(); 133 } catch (err) { 134 if (err !== "ripemd160() requires only 1 argument") { 135 throw err; 136 } 137 } 138 139 try { 140 var ret = crypto.nativeCrypto.ripemd(-121); 141 } catch (err) { 142 if (err.message !== "crypto.nativeCrypto.ripemd is not a function") { 143 throw err; 144 } 145 } 146 147 // negative alg 148 var ret = crypto.nativeCrypto.recoverAddress(-10, "564733f9f3e139b925cfb1e7e50ba8581e9107b13e4213f2e4708d9c284be75b", "d80e282d165f8c05d8581133df7af3c7c41d51ec7cd8470c18b84a31b9af6a9d1da876ab28a88b0226707744679d4e180691aca6bdef5827622396751a0670c101"); 149 eq(ret, null); 150 151 // invalid alg 152 ret = crypto.nativeCrypto.recoverAddress(10, "564733f9f3e139b925cfb1e7e50ba8581e9107b13e4213f2e4708d9c284be75b", "d80e282d165f8c05d8581133df7af3c7c41d51ec7cd8470c18b84a31b9af6a9d1da876ab28a88b0226707744679d4e180691aca6bdef5827622396751a0670c101"); 153 eq(ret, null); 154 155 // odd/invalid sign 156 ret = crypto.nativeCrypto.recoverAddress(1, "564733f9f3e139b925cfb1e7e50ba8581e9107b13e4213f2e4708d9c284be75b", "d80e282d165f8c05d8581133df7af3c7c41d51ec7cd"); 157 eq(ret, null); 158 159 // empty sign 160 ret = crypto.nativeCrypto.recoverAddress(1, "564733f9f3e139b925cfb1e7e50ba8581e9107b13e4213f2e4708d9c284be75b", ""); 161 eq(ret, null); 162 163 // empty hash 164 ret = crypto.nativeCrypto.recoverAddress(1, "", "d80e282d165f8c05d8581133df7af3c7c41d51ec7cd8470c18b84a31b9af6a9d1da876ab28a88b0226707744679d4e180691aca6bdef5827622396751a0670c101"); 165 eq(ret, null); 166 167 // odd/invalid hash 168 ret = crypto.nativeCrypto.recoverAddress(1, "564733f9f3e139b925cfb1e7e50ba8581e9107b13e421", "d80e282d165f8c05d8581133df7af3c7c41d51ec7cd8470c18b84a31b9af6a9d1da876ab28a88b0226707744679d4e180691aca6bdef5827622396751a0670c101"); 169 eq(ret, null); 170 171 try { 172 crypto.nativeCrypto.recoverAddress("", "564733f9f3e139b925cfb1e7e50ba8581e9107b13e4213f2e4708d9c284be75b", "d80e282d165f8c05d8581133df7af3c7c41d51ec7cd8470c18b84a31b9af6a9d1da876ab28a88b0226707744679d4e180691aca6bdef5827622396751a0670c101"); 173 } catch (err) { 174 if (err !== "recoverAddress(): 1st arg should be integer") { 175 throw err; 176 } 177 } 178 try { 179 crypto.nativeCrypto.recoverAddress(null, "564733f9f3e139b925cfb1e7e50ba8581e9107b13e4213f2e4708d9c284be75b", "d80e282d165f8c05d8581133df7af3c7c41d51ec7cd8470c18b84a31b9af6a9d1da876ab28a88b0226707744679d4e180691aca6bdef5827622396751a0670c101"); 180 } catch (err) { 181 if (err !== "recoverAddress(): 1st arg should be integer") { 182 throw err; 183 } 184 } 185 186 try { 187 crypto.nativeCrypto.recoverAddress(1, 123, "d80e282d165f8c05d8581133df7af3c7c41d51ec7cd8470c18b84a31b9af6a9d1da876ab28a88b0226707744679d4e180691aca6bdef5827622396751a0670c101"); 188 } catch (err) { 189 if (err !== "recoverAddress(): 2nd arg should be string") { 190 throw err; 191 } 192 } 193 194 try { 195 crypto.nativeCrypto.recoverAddress(1, "564733f9f3e139b925cfb1e7e50ba8581e9107b13e4213f2e4708d9c284be75b", null); 196 } catch (err) { 197 if (err !== "recoverAddress(): 3rd arg should be string") { 198 throw err; 199 } 200 } 201 202 try { 203 crypto.nativeCrypto.recoverAddress(1, "564733f9f3e139b925cfb1e7e50ba8581e9107b13e4213f2e4708d9c284be75b"); 204 } catch (err) { 205 if (err !== "recoverAddress() requires 3 arguments") { 206 throw err; 207 } 208 }