Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
this.GenerateSecret = function () {
var RIPEMD160 = require('ripemd160');
var crypto = require('crypto-browserify');
var secretBuffer = crypto.randomBytes(32);
var secret = secretBuffer.toString('hex');
var hashedSecret = new RIPEMD160().update(secretBuffer).digest('hex');
console.log("\nSecret:\t\t\t", secret);
console.log("\Hashed Secret:\t\t", hashedSecret, "\n");
return { "secret": secret, "hashedSecret": hashedSecret };
};
if (errors.length) {
return new ResponseEntity({ errors });
}
const service: ITransactionService = getTransactionServiceByType(data.type);
service.create(data);
const trs = new Transaction({
senderPublicKey: data.senderPublicKey,
senderAddress: data.senderAddress,
recipientAddress: data.recipientAddress,
type: data.type,
amount: data.amount,
fee: service.calculateFee(data, sender),
salt: cryptoBrowserify.randomBytes(SALT_LENGTH).toString('hex'),
});
trs.signature = this.sign(keyPair, trs);
trs.id = this.getId(trs);
return new ResponseEntity({ data: trs });
}
exportKeystore(privateKeyHex,password){
if (Utils.isEmpty(password) || password.length < 8){
throw new Error("password's length must be greater than 8")
}
if (Utils.isEmpty(privateKeyHex)){
throw new Error("private key must be not empty")
}
const salt = Cryp.randomBytes(32);
const iv = Cryp.randomBytes(16);
const cipherAlg = Config.iris.keystore.cipherAlg;
const kdf = Config.iris.keystore.kdf;
const address = this.import(privateKeyHex).address;
const kdfparams = {
dklen: 32,
salt: salt.toString("hex"),
c: Config.iris.keystore.c,
prf: "hmac-sha256"
};
const derivedKey = Cryp.pbkdf2Sync(Buffer.from(password), salt, kdfparams.c, kdfparams.dklen, "sha256");
const cipher = Cryp.createCipheriv(cipherAlg, derivedKey.slice(0, 16), iv);
if (!cipher) {
throw new Error("Unsupported cipher")
}
export const generateSecret = () => {
const secretBuffer = crypto.randomBytes(32);
const secret = secretBuffer.toString('hex');
const secretHash = ripemd160(secretBuffer);
return {
secret,
secretHash,
};
};
exportKeystore(privateKeyHex,password){
if (Utils.isEmpty(password) || password.length < 8){
throw new Error("password's length must be greater than 8")
}
if (Utils.isEmpty(privateKeyHex)){
throw new Error("private key must be not empty")
}
const salt = Cryp.randomBytes(32);
const iv = Cryp.randomBytes(16);
const cipherAlg = Config.iris.keystore.cipherAlg;
const kdf = Config.iris.keystore.kdf;
const address = this.import(privateKeyHex).address;
const kdfparams = {
dklen: 32,
salt: salt.toString("hex"),
c: Config.iris.keystore.c,
prf: "hmac-sha256"
};
const derivedKey = Cryp.pbkdf2Sync(Buffer.from(password), salt, kdfparams.c, kdfparams.dklen, "sha256");
const cipher = Cryp.createCipheriv(cipherAlg, derivedKey.slice(0, 16), iv);
if (!cipher) {
throw new Error("Unsupported cipher")
}
public static generateSecret(algo: AlgoTypes = AlgoTypes.Ripemd160) {
let algoInstance: IHashAlgo;
if (algo === AlgoTypes.Ripemd160) {
algoInstance = new Ripemd160();
} else if (algo === AlgoTypes.Hash160) {
algoInstance = new Hash160();
}
const secretBuffer = crypto.randomBytes(32);
const secret = secretBuffer.toString("hex");
const secretHash = algoInstance.hash(secretBuffer);
return new SecretResult(secret, secretHash);
}
}
if (!data.keypair) {
throw 'Invalid keypair';
}
let trs = {
type: data.type,
amount: 0,
senderPublicKey: data.sender.publicKey,
requesterPublicKey: data.requester ? data.requester.publicKey.toString('hex') : null,
timestamp: slots.getTime(),
asset: {},
stakedAmount: 0,
trsName: 'NA',
groupBonus: 0,
salt: cryptoBrowserify.randomBytes(16).toString('hex'),
reward: data.rewardPercentage || null
};
trs = await __private.types[trs.type].create.call(this, data, trs);
trs.signature = this.sign(data.keypair, trs);
if (data.sender.secondSignature && data.secondKeypair) {
trs.signSignature = this.sign(data.secondKeypair, trs);
}
trs.id = this.getId(trs);
trs.fee = __private.types[trs.type].calculateFee.call(this, trs, data.sender) || 0;
return trs;
};
};
const derivedKey = Cryp.pbkdf2Sync(Buffer.from(password), salt, kdfparams.c, kdfparams.dklen, "sha256");
const cipher = Cryp.createCipheriv(cipherAlg, derivedKey.slice(0, 16), iv);
if (!cipher) {
throw new Error("Unsupported cipher")
}
const cipherBuffer = Buffer.concat([cipher.update(Buffer.from(privateKeyHex, "hex")), cipher.final()]);
const bufferValue = Buffer.concat([derivedKey.slice(16, 32), cipherBuffer]);
let hashCiper = Cryp.createHash("sha256");
hashCiper.update(bufferValue);
const mac = hashCiper.digest().toString("hex");
return {
version: "1",
id: UUID.v4({
random: Cryp.randomBytes(16)
}),
address: address,
crypto: {
ciphertext: cipherBuffer.toString("hex"),
cipherparams: {
iv: iv.toString("hex")
},
cipher: cipherAlg,
kdf,
kdfparams: kdfparams,
mac: mac
}
}
}
importKeystore(keystore, password){
if (!data.keypair) {
throw 'Invalid keypair';
}
let trs = {
type: data.type,
amount: 0,
senderPublicKey: data.sender.publicKey,
requesterPublicKey: data.requester ? data.requester.publicKey.toString('hex') : null,
timestamp: slots.getTime(),
asset: {},
stakedAmount: 0,
trsName: 'NA',
groupBonus: 0,
salt: cryptoBrowserify.randomBytes(16).toString('hex'),
reward: data.rewardPercentage || null
};
trs = await __private.types[trs.type].create.call(self, data, trs);
trs.signature = self.sign(data.keypair, trs);
if (data.sender.secondSignature && data.secondKeypair) {
trs.signSignature = self.sign(data.secondKeypair, trs);
}
trs.id = self.getId(trs);
trs.fee = __private.types[trs.type].calculateFee.call(self, trs, data.sender) || 0;
return trs;
};
var generateSecret = exports.generateSecret = function generateSecret() {
var secretBuffer = crypto.randomBytes(32);
var secret = secretBuffer.toString('hex');
var secretHash = ripemd160(secretBuffer);
return {
secret: secret,
secretHash: secretHash
};
};