@casual-simulation/crypto-browser
v3.3.8
Published
An implemenation of @casual-simulation/crypto for web browsers.
Downloads
408
Keywords
Readme
Crypto Browser
Installation
npm install @casual-simulation/crypto-browser
Usage
// ES6-style imports are required.
// If you are running in an environment that does not support ES Modules,
// then use Webpack or Babel to transpile to the format you want. (like CommonJS)
import { BrowserSigningCryptoImpl } from '@casual-simulation/crypto-browser';
// Async is optional. Every method returns a promise.
async function demo() {
// Currently ECDSA-SHA256-NISTP256 is the only supported
// algorithm.
let algorithm = 'ECDSA-SHA256-NISTP256';
let crypto = new BrowserSigningCryptoImpl(algorithm);
console.log('Crypto Supported: ', crypto.supported());
// Generate a public-private key pair.
let [publicKey, privateKey] = await crypto.generateKeyPair();
// You can export the public and private keys to
// share them with other devices. (But really only share the public key)
let exportedPubKey = await crypto.exportKey(publicKey);
let exportedPrivateKey = await crypto.exportKey(privateKey);
// You can import keys that were exported using exportKey()
// via the importPrivateKey() and importPublicKey() methods.
console.log('Public Key: ', exportedPubKey);
console.log('Private Key: ', exportedPrivateKey);
// TODO: Save/share keys
// Any ArrayBuffer will work
let data = new Int32Array(100);
// If you're using webpack, enable the Buffer polyfil.
// This will let you convert strings to ArrayBuffer compatible
// objects using Buffer.from(str).
// Read More: https://webpack.js.org/configuration/node/
// Fill with pseudo-random data.
for (let i = 0; i < data.length; i++) {
data[i] = Math.floor(Math.random() * 100);
}
// Sign the data
let signature = crypto.sign(privateKey, data);
// TODO: Send or store the signature and data
// Verify the signature.
// Note that the data must be provided as well.
// This is because the signature does not store the data
// in a usable format.
let valid = crypto.verify(publicKey, signature, data);
console.log('Valid: ', valid);
}
demo();