0.17.0 • Published 8 months ago

stedy v0.17.0

Weekly downloads
-
License
Unlicense
Repository
github
Last release
8 months ago

Stedy (Beta)

Installation

$ npm i --save stedy

Basic usage

Elliptic curve cryptography

Deriving shared secrets

Perform a key exchange using the X25519 Elliptic Curve Diffie-Hellman function.

import { diffieHellman, generateKeyPair } from 'stedy'

const alice = await generateKeyPair()
const bob = await generateKeyPair()
const aliceSecret = await diffieHellman(alice.privateKey, bob.publicKey)
const bobSecret = await diffieHellman(bob.privateKey, alice.publicKey)
console.log({
  aliceSecret: aliceSecret.toString('base64'),
  bobSecret: bobSecret.toString('base64')
})
// {
//   aliceSecret: 'XJiXa0+XxscFfpud483+SQWdru48LNfZRxum2h4vEV8=',
//   bobSecret: 'XJiXa0+XxscFfpud483+SQWdru48LNfZRxum2h4vEV8='
// }

Verifying signatures

Create and verify Ed25519 signatures using the Edwards-curve Digital Signature Algorithm.

import { sign, generateSignKeyPair, verify } from 'stedy'
import { fromString } from 'stedy/bytes'

const { privateKey, publicKey } = await generateSignKeyPair()
const message = fromString('Hello World')
const signature = await sign(message, privateKey)
const verified = await verify(message, publicKey, signature)
console.log(verified)
// true

Secret key cryptography

Encrypt a message using AES in Galois/Counter Mode (GCM) with a 256-bit key.

import { decrypt, encrypt, generateKey, generateNonce } from 'stedy'
import { fromString } from 'stedy/bytes'

const message = fromString('Hello World')
const key = await generateKey()
const nonce = await generateNonce()
const ciphertext = await encrypt(key, nonce, message)
const decrypted = await decrypt(key, nonce, ciphertext)
console.log(decrypted.toString())
// Hello World

Hash digests

Compute the SHA-512 digest of a given message.

import { hash } from 'stedy'
import { fromString } from 'stedy/bytes'

const message = fromString('Hello World')
const digest = await hash(message)
console.log(digest.toString('base64'))
// LHT9F+2v2A6ER7DUZ0HuJDt+t03SFJoKsbkkb7MDgvJ+hT2FhXGeDmfL2g2qj1FnEGRhXWRa4nrLFb+xRH9Fmw==

Cryptographic HMAC digests

Generate a cryptographic HMAC hash using SHA-512.

import { hmac } from 'stedy'
import { fromString } from 'stedy/bytes'

const message = fromString('Hello World')
const key = fromString('secret')
const digest = await hmac(message, key)
console.log(digest.toString('base64'))
// CApRAydhlEam2xp19WEr2wM8jI66+E7uBbh/Z6VpvgmBACFtfVFX5VMtXh/e6lu75Tq5JAlI5jndeI4p19fk6w==

HMAC-based Key Derivation Function (HKDF)

Derive a key using the HKDF algorithm with SHA-512.

import { hkdf, generateRandomBytes } from 'stedy'
import { fromString } from 'stedy/bytes'

const inputKey = fromString('secret')
const salt = await generateRandomBytes(64)
const info = fromString('my-app')
const outputKey = await hkdf(inputKey, salt, info)
console.log(outputKey.toString('base64'))
// AT0yteGs4wtCRyeP9i76mK20YMfhXlhWO2E83eWQ6YGmPXjWZ92XZX6KfXKXF2DUb1EvYcJ82qHTssQmrJdunw==

Password-Based Key Derivation Function (PBKDF2)

Derive a key from a given password using PBKDF2 with SHA-512.

import { pbkdf2, generateRandomBytes } from 'stedy'
import { fromString } from 'stedy/bytes'

const password = fromString('horse-correct-battery-staple')
const salt = await generateRandomBytes(64)
const key = await pbkdf2(password, salt)
console.log(key.toString('base64'))
// xj4Rmi25dnoOX7Lf0zj/3bwE9PniTQsASu42bjZ96lEcwzo1UjCbTseifzDG6ShB4u1QRJUgFWlUYn6qfcf2XA==

Advanced usage

Elliptic curve cryptography

import { createCurve } from 'stedy'

const { diffieHellman, generateKeyPair, sign, generateSignKeyPair, verify } =
  createCurve('P-256', 'SHA-256')

Secret key cryptography

import { createCipher } from 'stedy'

const { decrypt, encrypt, generateKey, generateNonce } =
  createCipher('AES-192-CBC')

Hash-based functions

import { createHash } from 'stedy'

const { hash, hkdf, hmac, pbkdf2 } = createHash('SHA-384')
0.17.0

8 months ago

0.16.3

1 year ago

0.16.4

1 year ago

0.16.5

1 year ago

0.16.6

1 year ago

0.16.7

1 year ago

0.16.8

1 year ago

0.16.1

1 year ago

0.16.2

1 year ago

0.11.0

2 years ago

0.12.0

1 year ago

0.11.1

2 years ago

0.13.0

1 year ago

0.12.1

1 year ago

0.14.0

1 year ago

0.12.2

1 year ago

0.15.0

1 year ago

0.16.0

1 year ago

0.10.0

2 years ago

0.9.4

2 years ago

0.9.3

2 years ago

0.9.0

2 years ago

0.8.1

2 years ago

0.8.0

2 years ago

0.9.2

2 years ago

0.9.1

2 years ago

0.7.0

2 years ago

0.5.3

2 years ago

0.5.0

2 years ago

0.5.2

2 years ago

0.6.0

2 years ago

0.5.1

2 years ago

0.4.1

2 years ago

0.4.2

2 years ago

0.1.0

2 years ago

0.3.0

2 years ago

0.2.0

2 years ago

0.1.1

2 years ago

0.4.0

2 years ago

0.3.1

2 years ago

0.0.0

3 years ago