This repository has been archived by the owner on Oct 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |