diff --git a/javascript/README.md b/javascript/README.md new file mode 100644 index 0000000..1fe9f1b --- /dev/null +++ b/javascript/README.md @@ -0,0 +1,19 @@ +# Documentation + +## Encrypting Data + +### AES + +![AES Encryption](https://github.com/M3DZIK/libcrypto/assets/87065584/4c056daf-603e-45ac-9a52-a7af785d4128) + +## Password Hashing + +### Argon2 + +![Argon2 Password hashing](https://github.com/M3DZIK/libcrypto/assets/87065584/d90d9ad6-5717-47a2-a2cf-affc9ad3f502) + +## Key Exchange + +### X25519 + +![X25519 Key Exchange and AES Encryption](https://github.com/M3DZIK/libcrypto/assets/87065584/95fbff58-97fa-42c6-82d5-bee6a10f657c) diff --git a/javascript/src/index.ts b/javascript/src/index.ts new file mode 100644 index 0000000..0113242 --- /dev/null +++ b/javascript/src/index.ts @@ -0,0 +1,4 @@ +export * as aes from './aes'; +export * as argon2 from './argon2'; +export * as random from './random'; +export * as x25519 from './x25519'; diff --git a/javascript/src/readme.test.ts b/javascript/src/readme.test.ts new file mode 100644 index 0000000..314057e --- /dev/null +++ b/javascript/src/readme.test.ts @@ -0,0 +1,57 @@ +import { aes, argon2, random, x25519 } from "./"; + +describe("Readme Examples", () => { + test("Aes", () => { + const clearText = "hello world"; + // Key used for encryption (for example, argon2 hash in hex string) + const secretKey = "82fd4cefd6efde36171900b469bae4e06863cb70f80b4e216e44eeb0cf30460b"; + + // Encrypt using AES-GCM (AES-CBC is also available) + const cipherText = aes.encryptAesGcm(secretKey, clearText); + + // Decrypt cipher text + const decryptText = aes.decryptAesGcm(secretKey, cipherText); + + expect(decryptText).toBe(clearText); + }) + + test("Argon2", async () => { + // Compute a hash of password with random 16-byte salt + const hash = await argon2.ID({ + hashLength: 32, + iterations: 4, + memorySize: 65536, + parallelism: 4, + password: 'secret password', + salt: random.randBytes(16), + }); + + // Hash is a hex encoded string, can be used to Aes encrypt + + expect(hash).toHaveLength(32 * 2); + }) + + test("X25519 Exchange Keys", () => { + const bobKeyPair = x25519.generateKeyPair(); + const aliceKeyPair = x25519.generateKeyPair(); + + // Bob sends "hello world" to Alice + + // Compute shared secret between Bob and Alice + const sharedSecret_bob = x25519.computeSharedSecret(bobKeyPair.privateKey, aliceKeyPair.publicKey); + + // Encrypt message + const message = "hello world"; + const encryptedMessage = aes.encryptAesGcm(sharedSecret_bob, message); + + // Alice decrypts a message from Bob + + // The same as `sharedSecret_bob` + const sharedSecret_alice = x25519.computeSharedSecret(aliceKeyPair.privateKey, bobKeyPair.publicKey); + + // Decrypt message + const decryptedMessage = aes.decryptAesGcm(sharedSecret_alice, encryptedMessage); + + expect(decryptedMessage).toBe(message) + }) +})