From d2045b6450a21cba1886b8576f68c5eee7cc1243 Mon Sep 17 00:00:00 2001 From: jin Date: Wed, 13 Dec 2023 00:57:39 +0300 Subject: [PATCH] $mol_crypto: publish to npm new impl --- crypto/README.md | 84 +++++++--------------------------------- crypto/lib/lib.meta.tree | 5 +-- crypto/lib/package.json | 3 ++ 3 files changed, 20 insertions(+), 72 deletions(-) create mode 100644 crypto/lib/package.json diff --git a/crypto/README.md b/crypto/README.md index 22a9e7104bc..2e24258dbd4 100644 --- a/crypto/README.md +++ b/crypto/README.md @@ -2,29 +2,27 @@ Simple API for effective cross platform cryptography with minimal extra size. -## Symmetric encoding +## (a)Symmetric Encryption, Signing, Hashing -```typescript -// Any DataView or ArrayBuffer -const data = new Uint8Array([1,2,3]) - -// Should be unique for every encryption (but may be predictable) -const salt = $mol_crypto_salt() // 4 bytes -// Generates secret key -const Alice = await $mol_crypto_secret.generate() +```typescript +const Alice = await $mol_crypto_key_private.generate() // 96 B +const Bella = await $mol_crypto_key_private.generate() // 96 B -// Serialize secret key to ArrayBuffer (16 byte) -const key = await Alice.serial() +const secret = await $mol_crypto_secret.derive( + Alice.toString(), // 96 -> 129 B + Bella.public().toString() // 64 -> 86 B +) -// Reuse secret key from ArrayBuffer -const Bob = await $mol_crypto_secret.from( key ) +const data = new Uint8Array([ 1, 2, 3 ]) // 3 B +const salt = $mol_crypto_salt() // 12 B -// Use secret key and salt to encrypt data (4 bytes + data length ) -const closed = await Alice.encrypt( data, salt ) +const closed = await secret.encrypt( data, salt ) // 3+4 B +const digest = $mol_crypto_hash( closed ) // 20 B +const sign = await Alice.sign( digest ) // 64 B -// Use secret key and salt to decrypt data -const opened = await Bob.decrypt( closed, salt ) +const verified = await Alice.public().verify( digest, sign ) +const opened = await secret.decrypt( closed, salt ) // 3 B ``` ## Authentication @@ -34,58 +32,6 @@ const opened = await Bob.decrypt( closed, salt ) const Alice = await $mol_crypto_secret_id() ``` -## Asymmetric encoding - -```typescript -// Any DataView or ArrayBuffer -const data = new Uint8Array([1,2,3]) - -// Generates private-public key pair -const pair = await $mol_crypto_cipher_pair() - -// Serialize public key to ArrayBuffer (162 bytes) -const key_public = await pair.public.serial() - -// Serialize private key to ArrayBuffer (~640 bytes) -const key_private = await pair.private.serial() - -// Reuse keys from ArrayBuffer -const Alice = await $mol_crypto_cipher_public.from( key_public ) -const Bob = await $mol_crypto_cipher_private.from( key_private ) - -// Use public key to encrypt data (max 86 bytes input, 128 bytes output) -const closed = await Alice.encrypt( data ) - -// Use private key to decrypt data -const opened = await Bob.decrypt( closed ) -``` - -## Asymmetric signing - -```typescript -// Any DataView or ArrayBuffer -const data = new Uint8Array([1,2,3]) - -// Generates private-public key pair -const pair = await $mol_crypto_auditor_pair() - -// Serialize public key to ArrayBuffer (62 bytes) -const key_public = await pair.public.serial() - -// Serialize private key to ArrayBuffer (~195 bytes) -const key_private = await pair.private.serial() - -// Reuse keys from ArrayBuffer -const Alice = await $mol_crypto_auditor_public.from( key_public ) -const Bob = await $mol_crypto_auditor_private.from( key_private ) - -// Make sign for data (32 bytes) -const sign = await Alice.sign( data ) - -// Use sign to verify data -const = await Bob.verify( data, sign ) -``` - # Usage from NPM ``` diff --git a/crypto/lib/lib.meta.tree b/crypto/lib/lib.meta.tree index 68d4c872357..fe93f1417f7 100644 --- a/crypto/lib/lib.meta.tree +++ b/crypto/lib/lib.meta.tree @@ -1,5 +1,4 @@ -include \/mol/crypto/secret -include \/mol/crypto/cipher -include \/mol/crypto/auditor +include \/mol/crypto/secret/id +include \/mol/crypto/key include \/mol/crypto/hash include \/mol/crypto/salt diff --git a/crypto/lib/package.json b/crypto/lib/package.json new file mode 100644 index 00000000000..b2bf59e1291 --- /dev/null +++ b/crypto/lib/package.json @@ -0,0 +1,3 @@ +{ + "version": "0.1" +}