encryb / simplecrypto

Simple wrapper around WebCrypto implementations
Apache License 2.0
21 stars 4 forks source link

Promise support #3

Open ghost opened 6 years ago

ghost commented 6 years ago

I wish we could use this library with Promises and other ES6+ features like that:

simpleCrypto.asym.generateEncryptKey({ /* params here */ })
    .then((key, params) => simpleCrypto.asym.encrypt(key.publicKey, data))
    .then((enc, key) => simpleCrypto.asym.decrypt(usedKey.privateKey, enc))
    .then((data, key) => /* Do something with data and used key */)
    .catch(logError)

Or:

let key = await simpleCrypto.asym.generateEncryptKey({ /* params here */ });
let enc = await simpleCrypto.asym.encrypt(key.publicKey, data);
let dec = await simpleCrypto.asym.decrypt(key.privateKey, enc);
/* Do something with data and used key */

Instead of:

simpleCrypto.asym.generateEncryptKey(logError, function(keys){
    simpleCrypto.asym.encrypt(key.publicKey, data, logError, function(encrypted) {
        simpleCrypto.asym.decrypt(key.privateKey, encrypted, logError.bind(null, done), function(decrypted){
            // Do something
        });
    });
});
pwFoo commented 5 years ago

Would be nice to see simpleCrypt updated and maybe split it up in asym / sym files to reduce file size if just one part is needed (and there is no dependency between...).

I tried to test simpleCrypt with short and longer data string, but it fails with the existing example code? See #6