npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@safeheron/crypto-ecies

v1.0.0

Published

ecies

Downloads

144

Readme

crypto-ecies-js

Installation

npm install @safeheron/crypto-ecies

Examples

ECIES

  • Encrypt a string
        import * as cryptoJS from "crypto-js"
        import * as elliptic from 'elliptic'
        import * as assert from 'assert'
        import {Rand, Prime} from "@safeheron/crypto-rand"
        const P256 = elliptic.ec('p256')
        import {Hex, UrlBase64, CryptoJSBytes} from "@safeheron/crypto-utils"
        import {ECIES, AuthEnc, Authorize} from "@safeheron/crypto-ecies"
        
        let priv = await Rand.randomBN(32)
        console.log('priv: ', priv.toString(16))
        let pub = P256.g.mul(priv)
        let msg = 'hello world'
        let cypher = await ECIES.encryptString(pub, msg)
        console.log("cypher: ", Hex.fromBytes(cypher))
        let plain = ECIES.decryptString(priv, cypher)
        console.log("plain: ", plain)
        assert.equal(msg, plain)
  • Encrypt bytes
        import * as cryptoJS from "crypto-js"
        import * as elliptic from 'elliptic'
        import * as assert from 'assert'
        import {Rand, Prime} from "@safeheron/crypto-rand"
        const P256 = elliptic.ec('p256')
        import {Hex, UrlBase64, CryptoJSBytes} from "@safeheron/crypto-utils"
        import {ECIES, AuthEnc, Authorize} from "@safeheron/crypto-ecies"
        
        let priv = await Rand.randomBN(32)
        console.log('priv: ', priv.toString(16))
        let pub = P256.g.mul(priv)
        let data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
        let cypher = await ECIES.encryptBytes(pub, data)
        console.log('cypher: ', Hex.fromBytes(cypher))
        let plain = ECIES.decryptBytes(priv, cypher)
        console.log("plain data: ", Hex.fromBytes(plain))
        assert.equal(data.length, plain.length)
        for(let i = 0; i < data.length; i++){
            assert.equal(data.at(i), plain.at(i))
        }
  • Encrypt CryptoJSBytes
        import * as cryptoJS from "crypto-js"
        import * as elliptic from 'elliptic'
        import * as assert from 'assert'
        import {Rand, Prime} from "@safeheron/crypto-rand"
        const P256 = elliptic.ec('p256')
        import {Hex, UrlBase64, CryptoJSBytes} from "@safeheron/crypto-utils"
        import {ECIES, AuthEnc, Authorize} from "@safeheron/crypto-ecies"
        
        
        let priv = await Rand.randomBN(32)
        console.log('priv: ', priv.toString(16))
        let pub = P256.g.mul(priv)
        let msgHex = "123456789a"
        let data = cryptoJS.enc.Hex.parse(msgHex)
        let cypher = await ECIES.encryptCryptoJSBytes(pub, data)
        console.log('cypher: ', cryptoJS.enc.Hex.stringify(cypher))
        let plain = ECIES.decryptCryptoJSBytes(priv, cypher)
        let plainHex = cryptoJS.enc.Hex.stringify(plain)
        console.log("plainHex: ", plainHex)
        assert.equal(msgHex, plainHex)

Encryption with authorization

  • Encrypt a string
      let msg = 'hello, string'
      let localAuthPriv = await Rand.randomBN(32)
      let remoteAuthPriv = await Rand.randomBN(32)
      let localAuthPub = P256.g.mul(localAuthPriv)
      let remoteAuthPub = P256.g.mul(remoteAuthPriv)
      let cypherData = await AuthEnc.encryptString(localAuthPriv, remoteAuthPub, msg)
      console.log("cypherData:", cypherData)
      let [verifySig, plain] = AuthEnc.decryptString(remoteAuthPriv, localAuthPub, cypherData)
      if(verifySig){
          console.log("plainData:", plain)
      }
      assert(verifySig)
  • Encrypt bytes
        let data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
        let localAuthPriv = await Rand.randomBN(32)
        let remoteAuthPriv = await Rand.randomBN(32)
        let localAuthPub = P256.g.mul(localAuthPriv)
        let remoteAuthPub = P256.g.mul(remoteAuthPriv)
        let cypherData = await AuthEnc.encryptBytes(localAuthPriv, remoteAuthPub, data)
        console.log("cypherData:", cypherData)
        let [verifySig, plain] = AuthEnc.decryptBytes(remoteAuthPriv, localAuthPub, cypherData)
        if(verifySig){
            console.log("plainData:", Hex.fromBytes(plain))
        }
        assert(verifySig)
  • Encrypt CryptoJSBytes
        let msg = 'hello, WordArray(CryptoJSBytes)'
        let msgWordArray = cryptoJS.enc.Utf8.parse(msg)
        let localAuthPriv = await Rand.randomBN(32)
        let remoteAuthPriv = await Rand.randomBN(32)
        let localAuthPub = P256.g.mul(localAuthPriv)
        let remoteAuthPub = P256.g.mul(remoteAuthPriv)
        let cypherData = await AuthEnc.encryptCryptoJSBytes(localAuthPriv, remoteAuthPub, msgWordArray)
        console.log("cypherData:", cypherData)
        let [verifySig, plain] = AuthEnc.decryptCryptoJSBytes(remoteAuthPriv, localAuthPub, cypherData)
        if(verifySig){
            console.log("plainData:", Hex.fromCryptoJSBytes(plain))
        }
        assert(verifySig)

Authorization

        import {ECIES, AuthEnc, Authorize} from "@safeheron/crypto-ecies"

        let msg = 'hello'
        msg = cryptoJS.enc.Utf8.parse(msg)
        
        // local author key pair
        let authPriv = await Rand.randomBN(32)
        let authPub = P256.g.mul(authPriv)
        
        let signature = await Authorize.sign(authPriv, msg)
        console.log('sig:', signature)
        console.log('\n\n')
        let verifySig = Authorize.verify(authPub, msg, signature)
        assert(verifySig)