Skip to content
This repository has been archived by the owner on Oct 20, 2024. It is now read-only.

Commit

Permalink
javascript: Add readme
Browse files Browse the repository at this point in the history
  • Loading branch information
M3DZIK committed Sep 27, 2023
1 parent b38de3d commit 0299b12
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
19 changes: 19 additions & 0 deletions javascript/README.md
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 4 additions & 0 deletions javascript/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * as aes from './aes';
export * as argon2 from './argon2';
export * as random from './random';
export * as x25519 from './x25519';
57 changes: 57 additions & 0 deletions javascript/src/readme.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})

0 comments on commit 0299b12

Please sign in to comment.