Skip to content

Commit

Permalink
$mol_crypto: publish to npm new impl
Browse files Browse the repository at this point in the history
  • Loading branch information
jin committed Dec 12, 2023
1 parent fd64041 commit d2045b6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 72 deletions.
84 changes: 15 additions & 69 deletions crypto/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

```
Expand Down
5 changes: 2 additions & 3 deletions crypto/lib/lib.meta.tree
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions crypto/lib/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"version": "0.1"
}

0 comments on commit d2045b6

Please sign in to comment.