Update floCrypto.js
This commit is contained in:
parent
5164d51146
commit
60f1a2742d
549
src/floCrypto.js
549
src/floCrypto.js
@ -1,274 +1,339 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
(function(GLOBAL) {
|
(function(GLOBAL) {
|
||||||
var floCrypto = GLOBAL.floCrypto = {};
|
const floCrypto = GLOBAL.floCrypto = {
|
||||||
const p = BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 16);
|
|
||||||
const ecparams = EllipticCurve.getSECCurveByName("secp256k1");
|
|
||||||
|
|
||||||
function exponent1() {
|
util: {
|
||||||
return p.add(BigInteger.ONE).divide(BigInteger("4"));
|
p: BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 16),
|
||||||
};
|
|
||||||
|
|
||||||
function calculateY(x) {
|
ecparams: EllipticCurve.getSECCurveByName("secp256k1"),
|
||||||
let exp = exponent1();
|
|
||||||
// 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);
|
|
||||||
};
|
|
||||||
|
|
||||||
function getUncompressedPublicKey(compressedPublicKey) {
|
asciiAlternatives: `‘ '\n’ '\n“ "\n” "\n– --\n— ---\n≥ >=\n≤ <=\n≠ !=\n× *\n÷ /\n← <-\n→ ->\n↔ <->\n⇒ =>\n⇐ <=\n⇔ <=>`,
|
||||||
// Fetch x from compressedPublicKey
|
|
||||||
let pubKeyBytes = Crypto.util.hexToBytes(compressedPublicKey);
|
|
||||||
const prefix = pubKeyBytes.shift(); // remove prefix
|
|
||||||
let prefix_modulus = prefix % 2;
|
|
||||||
pubKeyBytes.unshift(0); // add prefix 0
|
|
||||||
let x = new BigInteger(pubKeyBytes);
|
|
||||||
let xDecimalValue = x.toString();
|
|
||||||
// Fetch y
|
|
||||||
let y = 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
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
function getSenderPublicKeyString() {
|
exponent1: function() {
|
||||||
privateKey = ellipticCurveEncryption.senderRandom();
|
return this.p.add(BigInteger.ONE).divide(BigInteger("4"))
|
||||||
senderPublicKeyString = ellipticCurveEncryption.senderPublicString(privateKey);
|
},
|
||||||
return {
|
|
||||||
privateKey: privateKey,
|
|
||||||
senderPublicKeyString: senderPublicKeyString
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
function deriveSharedKeySender(receiverCompressedPublicKey, senderPrivateKey) {
|
calculateY: function(x) {
|
||||||
try {
|
let p = this.p;
|
||||||
let receiverPublicKeyString = getUncompressedPublicKey(receiverCompressedPublicKey);
|
let exp = this.exponent1();
|
||||||
var senderDerivedKey = ellipticCurveEncryption.senderSharedKeyDerivation(
|
// x is x value of public key in BigInteger format without 02 or 03 or 04 prefix
|
||||||
receiverPublicKeyString.x, receiverPublicKeyString.y, senderPrivateKey);
|
return x.modPow(BigInteger("3"), p).add(BigInteger("7")).mod(p).modPow(exp, p)
|
||||||
return senderDerivedKey;
|
},
|
||||||
} catch (error) {
|
getUncompressedPublicKey: function(compressedPublicKey) {
|
||||||
return new Error(error);
|
const p = this.p;
|
||||||
};
|
// Fetch x from compressedPublicKey
|
||||||
};
|
let pubKeyBytes = Crypto.util.hexToBytes(compressedPublicKey);
|
||||||
|
const prefix = pubKeyBytes.shift() // remove prefix
|
||||||
|
let prefix_modulus = prefix % 2;
|
||||||
|
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
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
function deriveReceiverSharedKey(senderPublicKeyString, receiverPrivateKey) {
|
getSenderPublicKeyString: function() {
|
||||||
return ellipticCurveEncryption.receiverSharedKeyDerivation(
|
privateKey = ellipticCurveEncryption.senderRandom();
|
||||||
senderPublicKeyString.XValuePublicString,
|
senderPublicKeyString = ellipticCurveEncryption.senderPublicString(privateKey);
|
||||||
senderPublicKeyString.YValuePublicString, receiverPrivateKey);
|
return {
|
||||||
};
|
privateKey: privateKey,
|
||||||
|
senderPublicKeyString: senderPublicKeyString
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
function getReceiverPublicKeyString(privateKey) {
|
deriveSharedKeySender: function(receiverCompressedPublicKey, senderPrivateKey) {
|
||||||
return ellipticCurveEncryption.receiverPublicString(privateKey);
|
let receiverPublicKeyString = this.getUncompressedPublicKey(receiverCompressedPublicKey);
|
||||||
};
|
var senderDerivedKey = ellipticCurveEncryption.senderSharedKeyDerivation(
|
||||||
|
receiverPublicKeyString.x, receiverPublicKeyString.y, senderPrivateKey);
|
||||||
|
return senderDerivedKey;
|
||||||
|
},
|
||||||
|
|
||||||
function wifToDecimal(pk_wif, isPubKeyCompressed = false) {
|
deriveReceiverSharedKey: function(senderPublicKeyString, receiverPrivateKey) {
|
||||||
let pk = Bitcoin.Base58.decode(pk_wif);
|
return ellipticCurveEncryption.receiverSharedKeyDerivation(
|
||||||
pk.shift();
|
senderPublicKeyString.XValuePublicString, senderPublicKeyString.YValuePublicString, receiverPrivateKey);
|
||||||
pk.splice(-4, 4);
|
},
|
||||||
//If the private key corresponded to a compressed public key, also drop the last byte (it should be 0x01).
|
|
||||||
if (isPubKeyCompressed == true) pk.pop();
|
|
||||||
pk.unshift(0);
|
|
||||||
privateKeyDecimal = BigInteger(pk).toString();
|
|
||||||
privateKeyHex = Crypto.util.bytesToHex(pk);
|
|
||||||
return {
|
|
||||||
privateKeyDecimal: privateKeyDecimal,
|
|
||||||
privateKeyHex: privateKeyHex
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
|
getReceiverPublicKeyString: function(privateKey) {
|
||||||
|
return ellipticCurveEncryption.receiverPublicString(privateKey);
|
||||||
|
},
|
||||||
|
|
||||||
//generate a random Interger within range
|
deriveSharedKeyReceiver: function(senderPublicKeyString, receiverPrivateKey) {
|
||||||
floCrypto.randInt = function(min, max) {
|
return ellipticCurveEncryption.receiverSharedKeyDerivation(
|
||||||
min = Math.ceil(min);
|
senderPublicKeyString.XValuePublicString, senderPublicKeyString.YValuePublicString, receiverPrivateKey);
|
||||||
max = Math.floor(max);
|
},
|
||||||
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
||||||
};
|
|
||||||
|
|
||||||
//generate a random String within length (options : alphaNumeric chars only)
|
wifToDecimal: function(pk_wif, isPubKeyCompressed = false) {
|
||||||
floCrypto.randString = function(length, alphaNumeric = true) {
|
let pk = Bitcoin.Base58.decode(pk_wif)
|
||||||
var result = '';
|
pk.shift()
|
||||||
if (alphaNumeric)
|
pk.splice(-4, 4)
|
||||||
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
//If the private key corresponded to a compressed public key, also drop the last byte (it should be 0x01).
|
||||||
else
|
if (isPubKeyCompressed == true) pk.pop()
|
||||||
var characters =
|
pk.unshift(0)
|
||||||
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_+-./*?@#&$<>=[]{}():';
|
privateKeyDecimal = BigInteger(pk).toString()
|
||||||
for (var i = 0; i < length; i++)
|
privateKeyHex = Crypto.util.bytesToHex(pk)
|
||||||
result += characters.charAt(Math.floor(Math.random() * characters.length));
|
return {
|
||||||
return result;
|
privateKeyDecimal: privateKeyDecimal,
|
||||||
};
|
privateKeyHex: privateKeyHex
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
//Encrypt Data using public-key
|
//generate a random Interger within range
|
||||||
floCrypto.encryptData = function(data, publicKeyHex) {
|
randInt: function(min, max) {
|
||||||
var senderECKeyData = getSenderPublicKeyString();
|
min = Math.ceil(min);
|
||||||
var senderDerivedKey = deriveSharedKeySender(
|
max = Math.floor(max);
|
||||||
publicKeyHex, senderECKeyData.privateKey);
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
||||||
let senderKey = senderDerivedKey.XValue + senderDerivedKey.YValue;
|
},
|
||||||
let secret = Crypto.AES.encrypt(data, senderKey);
|
|
||||||
return {
|
|
||||||
secret: secret,
|
|
||||||
senderPublicKeyString: senderECKeyData.senderPublicKeyString
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
//Decrypt Data using private-key
|
//generate a random String within length (options : alphaNumeric chars only)
|
||||||
floCrypto.decryptData = function(data, privateKeyHex) {
|
randString: function(length, alphaNumeric = true) {
|
||||||
var receiverECKeyData = {};
|
var result = '';
|
||||||
if (typeof privateKeyHex !== "string") throw new Error("No private key found.");
|
if (alphaNumeric)
|
||||||
let privateKey = wifToDecimal(privateKeyHex, true);
|
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||||||
if (typeof privateKey.privateKeyDecimal !== "string") throw new Error(
|
else
|
||||||
"Failed to detremine your private key.");
|
var characters =
|
||||||
receiverECKeyData.privateKey = privateKey.privateKeyDecimal;
|
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_+-./*?@#&$<>=[]{}():';
|
||||||
var receiverDerivedKey = deriveReceiverSharedKey(
|
for (var i = 0; i < length; i++)
|
||||||
data.senderPublicKeyString, receiverECKeyData.privateKey);
|
result += characters.charAt(Math.floor(Math.random() * characters.length));
|
||||||
let receiverKey = receiverDerivedKey.XValue + receiverDerivedKey.YValue;
|
return result;
|
||||||
let decryptMsg = Crypto.AES.decrypt(data.secret, receiverKey);
|
},
|
||||||
return decryptMsg;
|
|
||||||
};
|
|
||||||
|
|
||||||
//Sign data using private-key
|
//Encrypt Data using public-key
|
||||||
floCrypto.signData = function(data, privateKeyHex) {
|
encryptData: function(data, receiverCompressedPublicKey) {
|
||||||
var key = new Bitcoin.ECKey(privateKeyHex);
|
var senderECKeyData = this.util.getSenderPublicKeyString();
|
||||||
if(key.priv === null)
|
var senderDerivedKey = this.util.deriveSharedKeySender(receiverCompressedPublicKey, senderECKeyData
|
||||||
return false;
|
.privateKey);
|
||||||
key.setCompressed(true);
|
let senderKey = senderDerivedKey.XValue + senderDerivedKey.YValue;
|
||||||
//var privateKeyArr = key.getBitcoinPrivateKeyByteArray();
|
let secret = Crypto.AES.encrypt(data, senderKey);
|
||||||
//var privateKey = BigInteger.fromByteArrayUnsigned(privateKeyArr);
|
|
||||||
var messageHash = Crypto.SHA256(data);
|
|
||||||
var messageHashBigInteger = new BigInteger(messageHash);
|
|
||||||
var messageSign = Bitcoin.ECDSA.sign(messageHashBigInteger, key.priv);
|
|
||||||
var sighex = Crypto.util.bytesToHex(messageSign);
|
|
||||||
return sighex;
|
|
||||||
};
|
|
||||||
|
|
||||||
//Verify signatue of the data using public-key
|
|
||||||
floCrypto.verifySign = function(data, signatureHex, publicKeyHex) {
|
|
||||||
var msgHash = Crypto.SHA256(data);
|
|
||||||
var messageHashBigInteger = new BigInteger(msgHash);
|
|
||||||
var sigBytes = Crypto.util.hexToBytes(signatureHex);
|
|
||||||
var signature = Bitcoin.ECDSA.parseSig(sigBytes);
|
|
||||||
var publicKeyPoint = ecparams.getCurve().decodePointHex(publicKeyHex);
|
|
||||||
var verify = Bitcoin.ECDSA.verifyRaw(messageHashBigInteger,
|
|
||||||
signature.r, signature.s, publicKeyPoint);
|
|
||||||
return verify;
|
|
||||||
};
|
|
||||||
|
|
||||||
//Generates a new flo ID and returns private-key, public-key and floID
|
|
||||||
floCrypto.generateNewID = function() {
|
|
||||||
try {
|
|
||||||
var key = new Bitcoin.ECKey(false);
|
|
||||||
key.setCompressed(true);
|
|
||||||
return {
|
return {
|
||||||
floID: key.getBitcoinAddress(),
|
secret: secret,
|
||||||
pubKey: key.getPubKeyHex(),
|
senderPublicKeyString: senderECKeyData.senderPublicKeyString
|
||||||
privKey: key.getBitcoinWalletImportFormat()
|
|
||||||
};
|
};
|
||||||
} catch (e) {
|
},
|
||||||
console.error(e);
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
//Returns public-key from private-key
|
//Decrypt Data using private-key
|
||||||
floCrypto.getPubKeyHex = function(privateKeyHex) {
|
decryptData: function(data, myPrivateKey) {
|
||||||
if (!privateKeyHex)
|
var receiverECKeyData = {};
|
||||||
return null;
|
if (typeof myPrivateKey !== "string") throw new Error("No private key found.");
|
||||||
var key = new Bitcoin.ECKey(privateKeyHex);
|
|
||||||
if (key.priv == null)
|
|
||||||
return null;
|
|
||||||
key.setCompressed(true);
|
|
||||||
return key.getPubKeyHex();
|
|
||||||
};
|
|
||||||
|
|
||||||
//Returns flo-ID from public-key or private-key
|
let privateKey = this.util.wifToDecimal(myPrivateKey, true);
|
||||||
floCrypto.getFloID = function(keyHex) {
|
if (typeof privateKey.privateKeyDecimal !== "string") throw new Error(
|
||||||
if (!keyHex)
|
"Failed to detremine your private key.");
|
||||||
return null;
|
receiverECKeyData.privateKey = privateKey.privateKeyDecimal;
|
||||||
try {
|
|
||||||
var key = new Bitcoin.ECKey(keyHex);
|
|
||||||
if (key.priv == null)
|
|
||||||
key.setPub(keyHex);
|
|
||||||
return key.getBitcoinAddress();
|
|
||||||
} catch (e) {
|
|
||||||
return null;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
//Verify the private-key for the given public-key or flo-ID
|
var receiverDerivedKey = this.util.deriveReceiverSharedKey(data.senderPublicKeyString,
|
||||||
floCrypto.verifyPrivKey = function(privateKeyHex, publicHex_ID) {
|
receiverECKeyData
|
||||||
if (!privateKeyHex || !publicHex_ID)
|
.privateKey);
|
||||||
return false;
|
|
||||||
try {
|
let receiverKey = receiverDerivedKey.XValue + receiverDerivedKey.YValue;
|
||||||
|
let decryptMsg = Crypto.AES.decrypt(data.secret, receiverKey);
|
||||||
|
return decryptMsg;
|
||||||
|
},
|
||||||
|
|
||||||
|
//Sign data using private-key
|
||||||
|
signData: function(data, privateKeyHex) {
|
||||||
|
var key = new Bitcoin.ECKey(privateKeyHex);
|
||||||
|
key.setCompressed(true);
|
||||||
|
|
||||||
|
var privateKeyArr = key.getBitcoinPrivateKeyByteArray();
|
||||||
|
privateKey = BigInteger.fromByteArrayUnsigned(privateKeyArr);
|
||||||
|
var messageHash = Crypto.SHA256(data);
|
||||||
|
|
||||||
|
var messageHashBigInteger = new BigInteger(messageHash);
|
||||||
|
var messageSign = Bitcoin.ECDSA.sign(messageHashBigInteger, key.priv);
|
||||||
|
|
||||||
|
var sighex = Crypto.util.bytesToHex(messageSign);
|
||||||
|
return sighex;
|
||||||
|
},
|
||||||
|
|
||||||
|
//Verify signatue of the data using public-key
|
||||||
|
verifySign: function(data, signatureHex, publicKeyHex) {
|
||||||
|
var msgHash = Crypto.SHA256(data);
|
||||||
|
var messageHashBigInteger = new BigInteger(msgHash);
|
||||||
|
|
||||||
|
var sigBytes = Crypto.util.hexToBytes(signatureHex);
|
||||||
|
var signature = Bitcoin.ECDSA.parseSig(sigBytes);
|
||||||
|
|
||||||
|
var publicKeyPoint = this.util.ecparams.getCurve().decodePointHex(publicKeyHex);
|
||||||
|
|
||||||
|
var verify = Bitcoin.ECDSA.verifyRaw(messageHashBigInteger, signature.r, signature.s,
|
||||||
|
publicKeyPoint);
|
||||||
|
return verify;
|
||||||
|
},
|
||||||
|
|
||||||
|
//Generates a new flo ID and returns private-key, public-key and floID
|
||||||
|
generateNewID: function() {
|
||||||
|
try {
|
||||||
|
var key = new Bitcoin.ECKey(false);
|
||||||
|
key.setCompressed(true);
|
||||||
|
return {
|
||||||
|
floID: key.getBitcoinAddress(),
|
||||||
|
pubKey: key.getPubKeyHex(),
|
||||||
|
privKey: key.getBitcoinWalletImportFormat()
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
//Returns public-key from private-key
|
||||||
|
getPubKeyHex: function(privateKeyHex) {
|
||||||
|
if (!privateKeyHex)
|
||||||
|
return null;
|
||||||
var key = new Bitcoin.ECKey(privateKeyHex);
|
var key = new Bitcoin.ECKey(privateKeyHex);
|
||||||
if (key.priv == null)
|
if (key.priv == null)
|
||||||
return false;
|
return null;
|
||||||
key.setCompressed(true);
|
key.setCompressed(true);
|
||||||
if (publicHex_ID === key.getBitcoinAddress())
|
return key.getPubKeyHex();
|
||||||
return true;
|
},
|
||||||
else if (publicHex_ID === key.getPubKeyHex())
|
|
||||||
return true;
|
//Returns flo-ID from public-key or private-key
|
||||||
else
|
getFloID: function(keyHex) {
|
||||||
|
if (!keyHex)
|
||||||
|
return null;
|
||||||
|
try {
|
||||||
|
var key = new Bitcoin.ECKey(keyHex);
|
||||||
|
if (key.priv == null)
|
||||||
|
key.setPub(keyHex);
|
||||||
|
return key.getBitcoinAddress();
|
||||||
|
} catch (e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
//Verify the private-key for the given public-key or flo-ID
|
||||||
|
verifyPrivKey: function(privateKeyHex, pubKey_floID, isfloID = true) {
|
||||||
|
if (!privateKeyHex || !pubKey_floID)
|
||||||
return false;
|
return false;
|
||||||
} catch (e) {
|
try {
|
||||||
console.error(e);
|
var key = new Bitcoin.ECKey(privateKeyHex);
|
||||||
};
|
if (key.priv == null)
|
||||||
};
|
return false;
|
||||||
|
key.setCompressed(true);
|
||||||
|
if (isfloID && pubKey_floID == key.getBitcoinAddress())
|
||||||
|
return true;
|
||||||
|
else if (!isfloID && pubKey_floID == key.getPubKeyHex())
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
//Check if the given Address is valid or not
|
//Check if the given Address is valid or not
|
||||||
floCrypto.validateAddr = function(inpAddr) {
|
validateAddr: function(inpAddr) {
|
||||||
if (!inpAddr)
|
if (!inpAddr)
|
||||||
return false;
|
return false;
|
||||||
try {
|
try {
|
||||||
var addr = new Bitcoin.Address(inpAddr);
|
var addr = new Bitcoin.Address(inpAddr);
|
||||||
return true;
|
return true;
|
||||||
} catch {
|
} catch {
|
||||||
return false;
|
return false;
|
||||||
};
|
}
|
||||||
};
|
},
|
||||||
|
|
||||||
//Split the str using shamir's Secret and Returns the shares
|
//Split the str using shamir's Secret and Returns the shares
|
||||||
floCrypto.createShamirsSecretShares = function(str, total_shares, threshold_limit) {
|
createShamirsSecretShares: function(str, total_shares, threshold_limit) {
|
||||||
try {
|
try {
|
||||||
if (str.length > 0) {
|
if (str.length > 0) {
|
||||||
var strHex = shamirSecretShare.str2hex(str);
|
var strHex = shamirSecretShare.str2hex(str);
|
||||||
return shamirSecretShare.share(strHex, total_shares, threshold_limit);
|
var shares = shamirSecretShare.share(strHex, total_shares, threshold_limit);
|
||||||
};
|
return shares;
|
||||||
return false;
|
}
|
||||||
} catch {
|
return false;
|
||||||
return false;
|
} catch {
|
||||||
};
|
return false
|
||||||
};
|
}
|
||||||
|
},
|
||||||
|
|
||||||
//Verifies the shares and str
|
//Verifies the shares and str
|
||||||
floCrypto.verifyShamirsSecret = function(sharesArray, str) {
|
verifyShamirsSecret: function(sharesArray, str) {
|
||||||
if(str == false)
|
return (str && this.retrieveShamirSecret(sharesArray) === str)
|
||||||
return false;
|
},
|
||||||
try {
|
|
||||||
if (sharesArray.length > 0) {
|
|
||||||
var comb = shamirSecretShare.combine(sharesArray.slice(0, sharesArray.length));
|
|
||||||
return (shamirSecretShare.hex2str(comb) === str ? true : false);
|
|
||||||
};
|
|
||||||
return false;
|
|
||||||
} catch {
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
//Returns the retrived secret by combining the shamirs shares
|
//Returns the retrived secret by combining the shamirs shares
|
||||||
floCrypto.retrieveShamirSecret = function(sharesArray) {
|
retrieveShamirSecret: function(sharesArray) {
|
||||||
try {
|
try {
|
||||||
if (sharesArray.length > 0) {
|
if (sharesArray.length > 0) {
|
||||||
var comb = shamirSecretShare.combine(sharesArray.slice(0, sharesArray.length));
|
var comb = shamirSecretShare.combine(sharesArray.slice(0, sharesArray.length));
|
||||||
return shamirSecretShare.hex2str(comb);
|
comb = shamirSecretShare.hex2str(comb);
|
||||||
};
|
return comb;
|
||||||
return false;
|
}
|
||||||
} catch {
|
return false;
|
||||||
return false;
|
} catch {
|
||||||
};
|
return false;
|
||||||
};
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
validateASCII: function(string, bool = true) {
|
||||||
|
if (typeof string !== "string")
|
||||||
|
return null;
|
||||||
|
if (bool) {
|
||||||
|
let x;
|
||||||
|
for (let i = 0; i < string.length; i++) {
|
||||||
|
x = string.charCodeAt(i);
|
||||||
|
if (x < 32 || x > 127)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
let x, invalids = {};
|
||||||
|
for (let i = 0; i < string.length; i++) {
|
||||||
|
x = string.charCodeAt(i);
|
||||||
|
if (x < 32 || x > 127)
|
||||||
|
if (x in invalids)
|
||||||
|
invalids[string[i]].push(i)
|
||||||
|
else
|
||||||
|
invalids[string[i]] = [i];
|
||||||
|
}
|
||||||
|
if (Object.keys(invalids).length)
|
||||||
|
return invalids;
|
||||||
|
else
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
convertToASCII: function(string, mode = 'soft-remove') {
|
||||||
|
let chars = this.validateASCII(string, false);
|
||||||
|
if (chars === true)
|
||||||
|
return string;
|
||||||
|
else if (chars === null)
|
||||||
|
return null;
|
||||||
|
let convertor, result = string,
|
||||||
|
refAlt = {};
|
||||||
|
this.util.asciiAlternatives.split('\n').forEach(a => refAlt[a[0]] = a.slice(2));
|
||||||
|
mode = mode.toLowerCase();
|
||||||
|
if (mode === "hard-unicode")
|
||||||
|
convertor = (c) => `\\u${('000'+c.charCodeAt().toString(16)).slice(-4)}`;
|
||||||
|
else if (mode === "soft-unicode")
|
||||||
|
convertor = (c) => refAlt[c] || `\\u${('000'+c.charCodeAt().toString(16)).slice(-4)}`;
|
||||||
|
else if (mode === "hard-remove")
|
||||||
|
convertor = c => "";
|
||||||
|
else if (mode === "soft-remove")
|
||||||
|
convertor = c => refAlt[c] || "";
|
||||||
|
else
|
||||||
|
return null;
|
||||||
|
for (let c in chars)
|
||||||
|
result = result.replaceAll(c, convertor(c));
|
||||||
|
return result;
|
||||||
|
},
|
||||||
|
|
||||||
|
revertUnicode: function(string) {
|
||||||
|
return string.replace(/\\u[\dA-F]{4}/gi,
|
||||||
|
m => String.fromCharCode(parseInt(m.replace(/\\u/g, ''), 16)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
})(typeof global !== "undefined" ? global : window);
|
})(typeof global !== "undefined" ? global : window);
|
||||||
Loading…
Reference in New Issue
Block a user