JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q22922F

Dead simple crypto module for Fognet

Package Exports

  • phog

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (phog) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

phog

crypto functions for fognet and the web

phog uses the Web Crypto API and base 58 encoding

open test.html in any browser to test

phog configs

const configs = {
  sign: {
    algo: {name: 'ECDSA', namedCurve: 'P-256'},
    usage: ['sign', 'verify'],
    format: {public:'spki', private:'pkcs8'}
  },
  derive: {
    algo: {name:'ECDH', namedCurve:'P-256'},
    usage: ['deriveKey'],
    format: {public:'spki', private:'pkcs8'}
  },
  encrypt: {
    algo: {name:'AES-GCM', length: 256},
    usage: ['encrypt', 'decrypt'],
    format: {public:'raw', private:'raw'},
    ivFunc: () => window.crypto.getRandomValues(new Uint8Array(12))
  }
}

api

keyGen (object)

Generate a key (returns a CryptoKey pair)

const key = await phog.keyGen(phog.configs.sign) // for signing
const key = await phog.keyGen(phog.configs.derive) // for deriving shared keys
// returns { publicKey:CryptoKey{}, privateKey:CryptoKey{} }

sign (string, CryptoKey)

Sign some text (returns base58-encoded signature string)

const sig = await phog.sign(txt, key.privateKey)

verify (string, string, CryptoKey)

Verify a signature (returns a bool)

const verified = await phog.verify(txt, sig, importedPubKey)

deriveKey (CryptoKey, CryptoKey)

Derive a shared symmetric key from your private key and another person's public key. (returns a CryptoKey)

const secret = await phog.deriveKey(key.privateKey, key.publicKey)

encrypt (string, CryptoKey)

Symmetrically encrypt some data (returns a base58-encoded string)

const encrypted = await phog.encrypt(txt, secret)

decrypt (string, CryptoKey)

Decrypt some data (returns a string)

const decrypted = await phog.decrypt(encrypted, secret)

exportKey (CryptoKey, object, string)

Export a public key to base58:

const keyString = await phog.exportKey(key.publicKey, phog.configs.sign)

To export a private key, you must specify 'private' as the last argument:

const keyString = await phog.exportKey(key.privateKey, phog.configs.sign, 'private')

importKey (string, object, string)

Import a base58-encoded public key (returns a CryptoKey)

const key = await phog.importKey(keyString, phog.configs.sign)