floCrypto improvement

moving non canon functions to util property
This commit is contained in:
sairajzero 2019-12-19 20:25:14 +05:30
parent 7a60c9f134
commit d09ada8261

View File

@ -5610,6 +5610,7 @@
/* FLO Crypto Operators*/
const floCrypto = {
util:{
p: BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 16),
ecparams: EllipticCurve.getSECCurveByName("secp256k1"),
@ -5624,30 +5625,8 @@
// x is x value of public key in BigInteger format without 02 or 03 or 04 prefix
return x.modPow(BigInteger("3"), p).add(BigInteger("7")).mod(p).modPow(exp, p)
},
//generate a random Interger within range
randInt: function(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
},
//generate a random String within length (options : alphaNumeric chars only)
randString: function (length, alphaNumeric = false) {
var result = '';
if(alphaNumeric)
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
else
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_+-./*?@#&$<>=[]{}():';
for ( var i = 0; i < length; i++ )
result += characters.charAt(Math.floor(Math.random() * characters.length));
return result;
},
getUncompressedPublicKey: function (compressedPublicKey) {
const p = this.p;
// Fetch x from compressedPublicKey
let pubKeyBytes = Crypto.util.hexToBytes(compressedPublicKey);
const prefix = pubKeyBytes.shift() // remove prefix
@ -5655,20 +5634,15 @@
pubKeyBytes.unshift(0) // add prefix 0
let x = new BigInteger(pubKeyBytes)
let xDecimalValue = x.toString()
// Fetch y
let y = this.calculateY(x);
let yDecimalValue = y.toString();
// verify y value
let resultBigInt = y.mod(BigInteger("2"));
let check = resultBigInt.toString() % 2;
if (prefix_modulus !== check) {
yDecimalValue = y.negate().mod(p).toString();
}
return {
x: xDecimalValue,
y: yDecimalValue
@ -5728,12 +5702,32 @@
privateKeyDecimal: privateKeyDecimal,
privateKeyHex: privateKeyHex
}
}
},
//generate a random Interger within range
randInt: function(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
},
//generate a random String within length (options : alphaNumeric chars only)
randString: function (length, alphaNumeric = false) {
var result = '';
if(alphaNumeric)
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
else
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_+-./*?@#&$<>=[]{}():';
for ( var i = 0; i < length; i++ )
result += characters.charAt(Math.floor(Math.random() * characters.length));
return result;
},
//Encrypt Data using public-key
encryptData: function (data, receiverCompressedPublicKey) {
var senderECKeyData = this.getSenderPublicKeyString();
var senderDerivedKey = this.deriveSharedKeySender(receiverCompressedPublicKey, senderECKeyData.privateKey);
var senderECKeyData = this.util.getSenderPublicKeyString();
var senderDerivedKey = this.util.deriveSharedKeySender(receiverCompressedPublicKey, senderECKeyData.privateKey);
let senderKey = senderDerivedKey.XValue + senderDerivedKey.YValue;
let secret = Crypto.AES.encrypt(data, senderKey);
return {
@ -5747,12 +5741,12 @@
var receiverECKeyData = {};
if (typeof myPrivateKey !== "string") throw new Error("No private key found.");
let privateKey = this.wifToDecimal(myPrivateKey, true);
let privateKey = this.util.wifToDecimal(myPrivateKey, true);
if (typeof privateKey.privateKeyDecimal !== "string") throw new Error(
"Failed to detremine your private key.");
receiverECKeyData.privateKey = privateKey.privateKeyDecimal;
var receiverDerivedKey = this.deriveReceiverSharedKey(data.senderPublicKeyString, receiverECKeyData
var receiverDerivedKey = this.util.deriveReceiverSharedKey(data.senderPublicKeyString, receiverECKeyData
.privateKey);
let receiverKey = receiverDerivedKey.XValue + receiverDerivedKey.YValue;
@ -5784,7 +5778,7 @@
var sigBytes = Crypto.util.hexToBytes(signatureHex);
var signature = Bitcoin.ECDSA.parseSig(sigBytes);
var publicKeyPoint = this.ecparams.getCurve().decodePointHex(publicKeyHex);
var publicKeyPoint = this.util.ecparams.getCurve().decodePointHex(publicKeyHex);
var verify = Bitcoin.ECDSA.verifyRaw(messageHashBigInteger, signature.r, signature.s,
publicKeyPoint);
@ -5853,6 +5847,7 @@
}
},
//Split the str using shamir's Secret and Returns the shares
createShamirsSecretShares: function (str, total_shares, threshold_limit) {
try{
if (str.length > 0) {
@ -5866,10 +5861,12 @@
}
},
//Verifies the shares and str
verifyShamirsSecret: function (sharesArray, str) {
return (str && this.retrieveShamirSecret(sharesArray) === str)
},
//Returns the retrived secret by combining the shamirs shares
retrieveShamirSecret: function (sharesArray) {
try{
if (sharesArray.length > 0) {