Open pwFoo opened 5 years ago
Got it fixed...
var data = new Uint8Array([5,4,3,2,1]);
//var data = ["Hi my friend!"];
var logError = function() {
console.log(arguments);
}
simpleCrypto.asym.generateEncryptKey(logError, function(key){
console.log("KEY:\n", key)
simpleCrypto.asym.encrypt(key.publicKey, data, logError, function(encrypted) {
console.log("ENCRYPTED:\n", encrypted);
simpleCrypto.asym.decrypt(key.privateKey, encrypted, logError.bind(null, "done"), function(decrypted){
console.log("DECRYPTED:\n", decrypted);
});
});
});
But it doesn't work with strings and needs "ArrayBuffer or ArrayBufferView". I'm know different libraries, but all work with strings (text for example). So how to use simplecrypto with (long) text messages (optional with embedded images)? Also found a newer repo here: https://github.com/Stanwar/simplecrypto
@encryb @Stanwar
Convert ArrayBuffer <-> String, found a optimized arrayBufferToString
to avoid the RangeError: Maximum call stack size exceeded
error.
var arrayBufferToString = function(buffer) {
//return String.fromCharCode.apply(null, new Uint8Array(buffer)); // RangeError: Maximum call stack size exceeded
var decoder = new TextDecoder ();
return decoder.decode (buffer);
}
var stringToArrayBuffer = function(str) {
return (new Uint8Array([].map.call(str,function(x){return x.charCodeAt(0)}))).buffer;
}
I generated a ~30MB string and ecrynt / sign keys, encrypted+signed and verified+decrypted the long data and it was lightning fast! 🥇 String generated with:
var string = "1234567890";
var iterations = 13;
for (var i = 0; i < iterations; i++) {
string += string+string;
}
Modified example code to add sign+verify and additional debugging output
simpleCrypto.asym.generateKeys(logError, function(keys){
console.log("ENCRYPTION KEY:\n", keys.encrypt);
console.log("SIGNING KEY:\n", keys.sign);
simpleCrypto.asym.encryptAndSign(keys.encrypt.publicKey, keys.sign.privateKey, data, logError, function(encrypted) {
console.log("ENCRYPTED:\n", encrypted);
simpleCrypto.asym.verifyAndDecrypt(keys.encrypt.privateKey, keys.sign.publicKey, encrypted, logError.bind(null, "done"), function(decrypted){
console.log("DECRYPTED ArrayBuffer:\n", decrypted);
console.log("DECRYPTED STRING:\n", arrayBufferToString(decrypted));
});
});
});
After get long text data / string working I'm really impressed about speed of simplecrypto! I have to read about save / load keys to reuse
Maybe I move to https://github.com/safebash/opencrypto because it's based on identical webcrypto api. But at the moment I have a problem with increasing data to encrypt because of DOMexception error: https://github.com/safebash/opencrypto/issues/5
Hi @encryb Error using the example asym code from readme:
source code
Looks like typo "key" instead of "keys"? Changed code:
New error message
Any idea why?