Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.2.0: Add BN, elliptic and nacl libs #70

Merged
merged 1 commit into from
Dec 15, 2022
Merged

0.2.0: Add BN, elliptic and nacl libs #70

merged 1 commit into from
Dec 15, 2022

Conversation

evgenykuzyakov
Copy link
Contributor

@evgenykuzyakov evgenykuzyakov commented Dec 14, 2022

Exposes new objects and libraries:

  • nacl - imports everything from tweetnacl npm
  • elliptic - imports everything from elliptic
  • BN - BN object from bn.js
  • Uint8Array - exposes Uint8Array

Buffer and Uint8Array are supported by deepCopy

Example:

const box = nacl.box;

const newNonce = () => nacl.randomBytes(box.nonceLength);
const generateKeyPair = () => box.keyPair();

const json = {
  a: "test",
};

const encrypt = (secretOrSharedKey, json, key) => {
  const nonce = newNonce();
  const messageUint8 = new Uint8Array(
    Buffer.from(JSON.stringify(json), "utf-8")
  );
  const encrypted = key
    ? box(messageUint8, nonce, key, secretOrSharedKey)
    : box.after(messageUint8, nonce, secretOrSharedKey);

  const fullMessage = new Uint8Array(nonce.length + encrypted.length);
  fullMessage.set(nonce);
  fullMessage.set(encrypted, nonce.length);

  const base64FullMessage = Buffer.from(fullMessage).toString("base64");
  return base64FullMessage;
};

const decrypt = (secretOrSharedKey, messageWithNonce, key) => {
  const messageWithNonceAsUint8Array = new Uint8Array(
    new Buffer(messageWithNonce, "base64")
  );
  const nonce = messageWithNonceAsUint8Array.slice(0, box.nonceLength);
  const message = messageWithNonceAsUint8Array.slice(
    box.nonceLength,
    messageWithNonce.length
  );

  const decrypted = key
    ? box.open(message, nonce, key, secretOrSharedKey)
    : box.open.after(message, nonce, secretOrSharedKey);

  if (!decrypted) {
    throw new Error("Could not decrypt message");
  }

  const base64DecryptedMessage = Buffer.from(decrypted).toString("utf-8");
  return JSON.parse(base64DecryptedMessage);
};

const obj = { hello: "world" };
const pairA = generateKeyPair();
const pairB = generateKeyPair();
const sharedA = box.before(pairB.publicKey, pairA.secretKey);
const sharedB = box.before(pairA.publicKey, pairB.secretKey);
const encrypted = encrypt(sharedA, obj);
const decrypted = decrypt(sharedB, encrypted);
console.log(obj, encrypted, decrypted);

return <pre>{JSON.stringify({ obj, encrypted, decrypted }, undefined, 2)}</pre>;

Fixes #69

@agileurbanite
Copy link
Contributor

LGTM just introducing new libs and modifying vm to respect that. One thing to note is how does the editor know about these new methods for the intellisense / code hints feature?

@agileurbanite
Copy link
Contributor

The day we introduce tests seems like a painful day...

@vans163
Copy link

vans163 commented Dec 14, 2022

Maybe some hash funcs to go along with crypto would be nice, https://github.com/Daninet/hash-wasm (particularly eyeing md5 (cloud storage), crc32c (new gen cloud storage), xxHash64 (next gen cloud storage), and blake3).

@evgenykuzyakov evgenykuzyakov merged commit 535736c into master Dec 15, 2022
@evgenykuzyakov evgenykuzyakov deleted the crypto branch December 15, 2022 20:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose cryptography libraries
3 participants