Closed mohitxskull closed 2 years ago
sodium.crypto_secretstream_xchacha20poly1305_KEYBYTES
Since you're using aead_chacha20poly1305
, that should be crypto_aead_chacha20poly1305_KEYBYTES
nonce = sodium.randombytes_buf(8, 'uint8array');
The nonce size should be crypto_aead_chacha20poly1305_NONCEBYTES
. I don't think 8
is correct.
sodium.crypto_pwhash_OPSLIMIT_INTERACTIVE,
sodium.crypto_pwhash_MEMLIMIT_INTERACTIVE,
sodium.crypto_pwhash_ALG_ARGON2ID13
You're mixing things as well. pwhash_OPSLIMIT_
(and _MEMLIMIT
) are for the default algorithm so the algorithm should be sodium.crypto_pwhash_ALG_DEFAULT
.
const DecRes = sodium.crypto_aead_chacha20poly1305_decrypt(
null,
INPUT,
null,
NONCE,
key,
'base64'
);
base64
is how the output of the function call will be encoded, not the input.
So INPUT
is still base64 encoded here.
Change it to:
const DecRes = sodium.crypto_aead_chacha20poly1305_decrypt(
null,
sodium.from_base64(INPUT),
null,
NONCE,
key
);
This is how I am using is right now and it's working great!
import _sodium from 'libsodium-wrappers';
const ChaCha20 = {
Enc: async (KEY: string, INPUT: string) => {
await _sodium.ready;
const Sodium = _sodium;
const Nonce = Sodium.randombytes_buf(
Sodium.crypto_aead_chacha20poly1305_NPUBBYTES
);
const EncRes = Sodium.crypto_aead_chacha20poly1305_encrypt(
INPUT,
null,
null,
Nonce,
Sodium.from_base64(KEY),
'base64'
);
return `${EncRes}$${Sodium.to_base64(Nonce)}`;
},
Dec: async (KEY: string, INPUT: string) => {
await _sodium.ready;
const Sodium = _sodium;
const Nonce = INPUT.split('$')[1];
const Cipher = INPUT.split('$')[0];
const DecRes = Sodium.crypto_aead_chacha20poly1305_decrypt(
null,
Sodium.from_base64(Cipher),
null,
Sodium.from_base64(Nonce),
Sodium.from_base64(KEY),
'text'
);
return DecRes;
},
};
export default ChaCha20;
Glad to hear that it works!
Encryption is working!
But when i try to decrypt my data, it throw's an error!