|
|
@ -191,9 +191,22 @@ function bytesFromWords (wordArray) { |
|
|
|
function bytesFromBigInt (bigInt, len) { |
|
|
|
function bytesFromBigInt (bigInt, len) { |
|
|
|
var bytes = bigInt.toByteArray(); |
|
|
|
var bytes = bigInt.toByteArray(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (len && bytes.length < len) { |
|
|
|
|
|
|
|
var padding = []; |
|
|
|
|
|
|
|
for (var i = 0, needPadding = len - bytes.length; i < needPadding; i++) { |
|
|
|
|
|
|
|
padding[i] = 0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (bytes instanceof ArrayBuffer) { |
|
|
|
|
|
|
|
bytes = bufferConcat(padding, bytes); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
bytes = padding.concat(bytes); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
while (!bytes[0] && (!len || bytes.length > len)) { |
|
|
|
while (!bytes[0] && (!len || bytes.length > len)) { |
|
|
|
bytes = bytes.slice(1); |
|
|
|
bytes = bytes.slice(1); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return bytes; |
|
|
|
return bytes; |
|
|
|
} |
|
|
|
} |
|
|
@ -320,13 +333,19 @@ function rsaEncrypt (publicKey, bytes) { |
|
|
|
return encryptedBytes; |
|
|
|
return encryptedBytes; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function addPadding(bytes, blockSize) { |
|
|
|
function addPadding(bytes, blockSize, zeroes) { |
|
|
|
blockSize = blockSize || 16; |
|
|
|
blockSize = blockSize || 16; |
|
|
|
var len = bytes.byteLength || bytes.length; |
|
|
|
var len = bytes.byteLength || bytes.length; |
|
|
|
var needPadding = blockSize - (len % blockSize); |
|
|
|
var needPadding = blockSize - (len % blockSize); |
|
|
|
if (needPadding > 0 && needPadding < blockSize) { |
|
|
|
if (needPadding > 0 && needPadding < blockSize) { |
|
|
|
var padding = new Array(needPadding); |
|
|
|
var padding = new Array(needPadding); |
|
|
|
|
|
|
|
if (zeroes) { |
|
|
|
|
|
|
|
for (var i = 0; i < needPadding; i++) { |
|
|
|
|
|
|
|
padding[i] = 0 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
(new SecureRandom()).nextBytes(padding); |
|
|
|
(new SecureRandom()).nextBytes(padding); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (bytes instanceof ArrayBuffer) { |
|
|
|
if (bytes instanceof ArrayBuffer) { |
|
|
|
bytes = bufferConcat(bytes, padding); |
|
|
|
bytes = bufferConcat(bytes, padding); |
|
|
@ -635,5 +654,5 @@ function bytesModPow (x, y, m) { |
|
|
|
console.error('mod pow error', e); |
|
|
|
console.error('mod pow error', e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return bytesFromBigInt(new BigInteger(x).modPow(new BigInteger(y), new BigInteger(m))); |
|
|
|
return bytesFromBigInt(new BigInteger(x).modPow(new BigInteger(y), new BigInteger(m)), 256); |
|
|
|
} |
|
|
|
} |
|
|
|