phog
v0.0.3
Published
Dead simple crypto module for Fognet
Downloads
6
Maintainers
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)