src/crypto/cryptoHelpers.js
function(recipientPrivate, senderPublic, _payload) {
    // Errors
    if(!recipientPrivate || !senderPublic || !_payload) throw new Error('Missing argument !');
    if (!Helpers.isPrivateKeyValid(recipientPrivate)) throw new Error('Private key is not valid !');
    if (!Helpers.isPublicKeyValid(senderPublic)) throw new Error('Public key is not valid !');
    // Processing
    let binPayload = convert.hex2ua(_payload);
    let salt = new Uint8Array(binPayload.buffer, 0, 32);
    let iv = new Uint8Array(binPayload.buffer, 32, 16);
    let payload = new Uint8Array(binPayload.buffer, 48);
    let sk = convert.hex2ua_reversed(recipientPrivate);
    let pk = convert.hex2ua(senderPublic);
    let shared = new Uint8Array(32);
    let r = key_derive(shared, salt, sk, pk);
    let encKey = r;
    let encIv = {
        iv: convert.ua2words(iv, 16)
    };
    let encrypted = {
        'ciphertext': convert.ua2words(payload, payload.length)
    };
    let plain = CryptoJS.AES.decrypt(encrypted, encKey, encIv);
    // Result
    let hexplain = CryptoJS.enc.Hex.stringify(plain);
    return hexplain;
}
function(recipientPrivate, senderPublic, _payload) {
    // Errors
    if(!recipientPrivate || !senderPublic || !_payload) throw new Error('Missing argument !');
    if (!Helpers.isPrivateKeyValid(recipientPrivate)) throw new Error('Private key is not valid !');
    if (!Helpers.isPublicKeyValid(senderPublic)) throw new Error('Public key is not valid !');
    // Processing
    let binPayload = convert.hex2ua(_payload);
    let salt = new Uint8Array(binPayload.buffer, 0, 32);
    let iv = new Uint8Array(binPayload.buffer, 32, 16);
    let payload = new Uint8Array(binPayload.buffer, 48);
    let sk = convert.hex2ua_reversed(recipientPrivate);
    let pk = convert.hex2ua(senderPublic);
    let shared = new Uint8Array(32);
    let r = key_derive(shared, salt, sk, pk);
    let encKey = r;
    let encIv = {
        iv: convert.ua2words(iv, 16)
    };
    let encrypted = {
        'ciphertext': convert.ua2words(payload, payload.length)
    };
    let plain = CryptoJS.AES.decrypt(encrypted, encKey, encIv);
    // Result
    let hexplain = CryptoJS.enc.Hex.stringify(plain);
    return hexplain;
}
function ( recipientPrivate , senderPublic , _payload ) { // Errors if ( ! recipientPrivate || ! senderPublic || ! _payload ) throw new Error ( 'Missing argument !' ) ; if ( ! Helpers . isPrivateKeyValid ( recipientPrivate ) ) throw new Error ( 'Private key is not valid !' ) ; if ( ! Helpers . isPublicKeyValid ( senderPublic ) ) throw new Error ( 'Public key is not valid !' ) ; // Processing let binPayload = convert . hex2ua ( _payload ) ; let salt = new Uint8Array ( binPayload . buffer , 0 , 32 ) ; let iv = new Uint8Array ( binPayload . buffer , 32 , 16 ) ; let payload = new Uint8Array ( binPayload . buffer , 48 ) ; let sk = convert . hex2ua_reversed ( recipientPrivate ) ; let pk = convert . hex2ua ( senderPublic ) ; let shared = new Uint8Array ( 32 ) ; let r = key_derive ( shared , salt , sk , pk ) ; let encKey = r ; let encIv = { iv : convert . ua2words ( iv , 16 ) } ; let encrypted = { 'ciphertext' : convert . ua2words ( payload , payload . length ) } ; let plain = CryptoJS . AES . decrypt ( encrypted , encKey , encIv ) ; // Result let hexplain = CryptoJS . enc . Hex . stringify ( plain ) ; return hexplain ; }
Decode an encrypted message payload

@param {string} recipientPrivate - A recipient private key
@param {string} senderPublic - A sender public key
@param {string} _payload - An encrypted message payload

@return {string} - The decoded payload as hex
Decode an encrypted message payload