New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@casual-simulation/crypto-browser

Package Overview
Dependencies
Maintainers
2
Versions
84
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@casual-simulation/crypto-browser

An implemenation of @casual-simulation/crypto for web browsers.

  • 0.11.20
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
11
increased by120%
Maintainers
2
Weekly downloads
 
Created
Source

Crypto Browser

npm (scoped)

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();

Keywords

FAQs

Package last updated on 13 Jan 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc