github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/v8/lib/1.0.5/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 const HexStringRegex = /^[0-9a-fA-F]+$/; 22 23 var Crypto = function() { 24 Object.defineProperty(this, "nativeCrypto", { 25 configurable: false, 26 enumerable: false, 27 get: function(){ 28 return _native_crypto; 29 } 30 }); 31 }; 32 33 Crypto.prototype = { 34 35 // case sensitive 36 sha256: function(data) { 37 if (typeof data !== "string") { 38 throw new Error("input must be string"); 39 } 40 // any string 41 return this.nativeCrypto.sha256(data); 42 }, 43 44 // case sensitive 45 sha3256: function(data) { 46 if (typeof data !== "string") { 47 throw new Error("input must be string"); 48 } 49 // any string 50 return this.nativeCrypto.sha3256(data); 51 }, 52 53 // case sensitive 54 ripemd160: function(data) { 55 if (typeof data !== "string") { 56 throw new Error("input must be string"); 57 } 58 // any string 59 return this.nativeCrypto.ripemd160(data); 60 }, 61 62 // case insensitive 63 recoverAddress: function(alg, hash, sign) { 64 if (!Number.isSafeInteger(alg) || alg < 0) { 65 throw new Error("alg must be non-negative integer"); 66 } 67 68 if (typeof hash !== "string" || !HexStringRegex.test(hash) 69 || typeof sign !== "string" || !HexStringRegex.test(sign)) { 70 throw new Error("hash & sign must be hex string"); 71 } 72 // alg: 1 73 // hash: sha3256 hex string, 64 chars 74 // sign: cipher hex string by private key, 130 chars 75 return this.nativeCrypto.recoverAddress(alg, hash, sign); 76 }, 77 78 // case sensitive 79 md5: function(data) { 80 if (typeof data !== "string") { 81 throw new Error("input must be string"); 82 } 83 // any string 84 return this.nativeCrypto.md5(data); 85 }, 86 87 // case sensitive 88 base64: function(data) { 89 if (typeof data !== "string") { 90 throw new Error("input must be string"); 91 } 92 // any string 93 return this.nativeCrypto.base64(data); 94 } 95 }; 96 97 module.exports = new Crypto();