Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
static GetNep2FromPrivateKey(prikey, passphrase, n = 16384, r = 8, p = 8, callback) {
let that = this;
var pubkey = Helper.GetPublicKeyFromPrivateKey(prikey);
let addr = Helper.GetAddressFromPublicKey(pubkey);
const strkey = SHA256(SHA256(addr).toString()).toString().slice(0, 4);
let uint8pass = this.String2Bytes(passphrase);
console.log('strkey = ' + prikey);
scrypt.default(uint8pass, strkey, {
logN: 5,
r: r,
p: p,
dkLen: 64,
interruptStep: 1000,
encoding: 'hash'
}, function (res) {
var u8dk = new Uint8Array(res);
var derivedhalf1 = u8dk.subarray(0, 32);
var derivedhalf2 = u8dk.subarray(32, 64);
var u8xor = new Uint8Array(32);
for (var i = 0; i < 32; i++) {
u8xor[i] = prikey[i] ^ derivedhalf1[i];
const decrypt = (encryptedKey, keyphrase, progressCallback) => {
const assembled = ab2hexstring(bs58check.decode(encryptedKey))
const addressHash = assembled.substr(6, 8)
const encrypted = assembled.substr(-64)
const derived = scrypt.hashSync(Buffer.from(keyphrase, 'utf8'), Buffer.from(addressHash, 'hex'), scrypt_options, progressCallback).toString('hex')
const derived1 = derived.slice(0, 64)
const derived2 = derived.slice(64)
const ciphertext = { ciphertext: enc.Hex.parse(encrypted), salt: "" }
const decrypted = AES.decrypt(ciphertext, enc.Hex.parse(derived2), { mode: C.mode.ECB, padding: C.pad.NoPadding })
const privateKey = hexXor(decrypted.toString(), derived1)
console.log("private key dec", privateKey);
const address = getAccountsFromPrivateKey(privateKey)[0].address
const newAddressHash = SHA256(SHA256(enc.Latin1.parse(address))).toString().slice(0, 8)
if (addressHash !== newAddressHash) throw new Error("Wrong Password!")
return getWIFFromPrivateKey(Buffer.from(privateKey, 'hex'));
};
export const getSHA256 = async (value) => {
return await CryptoJS.SHA256(value).toString();
}
getSignContent() {
const data = this.serializeUnsignedData();
const ProgramHexString = cryptoJS.enc.Hex.parse(data);
const ProgramSha256 = cryptoJS.SHA256(ProgramHexString).toString();
const ProgramSha2562 = cryptoJS.SHA256(cryptoJS.enc.Hex.parse(ProgramSha256)).toString();
return ProgramSha2562;
}
function calculateHash ({index, prevHash, time, transactions, nonce}) {
return CryptoJS.SHA256(JSON.stringify({index, prevHash, time, transactions, nonce})).toString()
}
decrypt: (encryptedKey, keyphrase, progressCallback) => {
const assembled = toHexString(bs58check.decode(encryptedKey))
const addressHash = assembled.substr(6, 8)
const encrypted = assembled.substr(-64)
const derived = scrypt(Buffer.from(keyphrase, 'utf8'), Buffer.from(addressHash, 'hex'), 16384, 8, 8, 64, progressCallback).toString('hex')
const derived1 = derived.slice(0, 64)
const derived2 = derived.slice(64)
const ciphertext = { ciphertext: enc.Hex.parse(encrypted), salt: "" }
const decrypted = AES.decrypt(ciphertext, enc.Hex.parse(derived2), { mode: C.mode.ECB, padding: C.pad.NoPadding })
const privateKey = hexXor(decrypted.toString(), derived1)
const address = crypto.getAddrFromPri(privateKey)
const newAddressHash = SHA256(SHA256(enc.Latin1.parse(address))).toString().slice(0, 8)
if (addressHash !== newAddressHash) throw new Error("Wrong Password!")
return privateKey
}
}
hash: function (data) {
try {
let dataHash = cryptoJS.SHA256(data).toString(cryptoJS.enc.Hex);
return dataHash;
} catch (error) {
throw new Error('Couldn\'t hash the data')
}
},
encrypt: function (data, password) {
toSHA256(text) {
if (CryptoJS && CryptoJS.SHA256)
return CryptoJS.SHA256(text).toString();
}
function calculateHash(index, previousHash, timestamp, data, difficulty, nonce) {
return CryptoJS.SHA256(index + previousHash + timestamp + data + difficulty + nonce).toString();
}
export function tripleSha256(str) {
return CryptoJS.SHA256(CryptoJS.SHA256(CryptoJS.SHA256(str))).toString()
}