Socket
Socket
Sign inDemoInstall

@arcblock/mcrypto

Package Overview
Dependencies
Maintainers
1
Versions
103
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@arcblock/mcrypto - npm Package Compare versions

Comparing version 0.20.0 to 0.20.5

49

lib/hasher/keccak.js
const sha3 = require('js-sha3');
const { isHexStrict, hexToBytes } = require('@arcblock/forge-util');
/**
* Keccak support with different hash length
*
* @class KeccakHasher
*/
class KeccakHasher {

@@ -23,2 +28,46 @@ constructor() {

/**
* Do hash with length of 224
*
* @function
* @name KeccakHasher#hash224
* @param {string} input - data to hash in hex format
* @param {number} [round=1] - how many round to do the hash, larger = safer = slower
* @returns {string} hash in hex format
* @memberof KeccakHasher
*/
/**
* Do hash with length of 256
*
* @function
* @name KeccakHasher#hash256
* @param {string} input - data to hash in hex format
* @param {number} [round=1] - how many round to do the hash, larger = safer = slower
* @returns {string} hash in hex format
* @memberof KeccakHasher
*/
/**
* Do hash with length of 384
*
* @function
* @name KeccakHasher#hash384
* @param {string} input - data to hash in hex format
* @param {number} [round=1] - how many round to do the hash, larger = safer = slower
* @returns {string} hash in hex format
* @memberof KeccakHasher
*/
/**
* Do hash with length of 512
*
* @function
* @name KeccakHasher#hash512
* @param {string} input - data to hash in hex format
* @param {number} [round=1] - how many round to do the hash, larger = safer = slower
* @returns {string} hash in hex format
* @memberof KeccakHasher
*/
module.exports = new KeccakHasher();

@@ -10,2 +10,7 @@ const { isHexStrict, hexToBytes } = require('@arcblock/forge-util');

/**
* Sha2 support with different hash length
*
* @class
*/
class Sha2Hasher {

@@ -30,2 +35,45 @@ constructor() {

/**
* Do hash with length of 224
*
* @function
* @name Sha2Hasher#hash224
* @param {string} input - data to hash in hex format
* @param {number} [round=2] - how many round to do the hash, larger = safer = slower
* @returns {string} hash in hex format
* @memberof Sha2Hasher
*/
/**
* Do hash with length of 256
*
* @function
* @name Sha2Hasher#hash256
* @param {string} input - data to hash in hex format
* @param {number} [round=2] - how many round to do the hash, larger = safer = slower
* @returns {string} hash in hex format
* @memberof Sha2Hasher
*/
/**
* Do hash with length of 384
*
* @function
* @name Sha2Hasher#hash384
* @param {string} input - data to hash in hex format
* @param {number} [round=2] - how many round to do the hash, larger = safer = slower
* @returns {string} hash in hex format
* @memberof Sha2Hasher
*/
/**
* Do hash with length of 512
*
* @function
* @name Sha2Hasher#hash512
* @param {string} input - data to hash in hex format
* @param {number} [round=2] - how many round to do the hash, larger = safer = slower
* @returns {string} hash in hex format
* @memberof Sha2Hasher
*/
module.exports = new Sha2Hasher();
const sha3 = require('js-sha3');
const { isHexStrict, hexToBytes } = require('@arcblock/forge-util');
/**
* Sha3 support with different hash length
*
* @class Sha3Hasher
*/
class Sha3Hasher {

@@ -23,2 +28,46 @@ constructor() {

/**
* Do hash with length of 224
*
* @function
* @name Sha3Hasher#hash224
* @param {string} input - data to hash in hex format
* @param {number} [round=1] - how many round to do the hash, larger = safer = slower
* @returns {string} hash in hex format
* @memberof Sha3Hasher
*/
/**
* Do hash with length of 256
*
* @function
* @name Sha3Hasher#hash256
* @param {string} input - data to hash in hex format
* @param {number} [round=1] - how many round to do the hash, larger = safer = slower
* @returns {string} hash in hex format
* @memberof Sha3Hasher
*/
/**
* Do hash with length of 384
*
* @function
* @name Sha3Hasher#hash384
* @param {string} input - data to hash in hex format
* @param {number} [round=1] - how many round to do the hash, larger = safer = slower
* @returns {string} hash in hex format
* @memberof Sha3Hasher
*/
/**
* Do hash with length of 512
*
* @function
* @name Sha3Hasher#hash512
* @param {string} input - data to hash in hex format
* @param {number} [round=1] - how many round to do the hash, larger = safer = slower
* @returns {string} hash in hex format
* @memberof Sha3Hasher
*/
module.exports = new Sha3Hasher();

108

lib/index.js

@@ -0,1 +1,18 @@

/**
* @fileOverview Forge [mcrypto](https://github.com/ArcBlock/mcrypto) implementation for javascript.
* Just a wrapper around existing javascript crypto libraries, implementation details can be found:
*
* - Signer
* - Ed25519 {@link Ed25519Signer}
* - Secp256k1 {@link Secp256k1Signer}
* - Hasher
* - SHA2 {@link Sha2Hasher}
* - SHA3 {@link Sha3Hasher}
* - KECCAK {@link KecaakHasher}
*
* @module @arcblock/mcrypto
* @example
yarn add @arcblock/mcrypto
*/
// FIXME: enum definition of forge-abi and abt-did-elixir are not exactly the same

@@ -34,3 +51,11 @@ const types = {

const Mcrypto = {
const Mcrypto = (module.exports = {
/**
* Contains all supported signers, eg: `Ed25519` and `Secp256k1`
*
* @readonly
* @type {object}
* @name Signer
* @static
*/
Signer: {

@@ -40,2 +65,11 @@ Ed25519: require('./signer/ed25519'),

},
/**
* Contains all supported hasher, eg: `SHA2`,`SHA3` and `Keccak`, each of them supports `hash224`, `hash256`, `hash384`, `hash512`
*
* @readonly
* @type {object}
* @name Hasher
* @static
*/
Hasher: {

@@ -46,2 +80,9 @@ SHA2: require('./hasher/sha2'),

},
/**
* Contains all supported crypter, eg: `AES`, each of them supports `encrypt`, `decrypt`
*
* @name Crypter
* @static
*/
Crypter: {

@@ -51,3 +92,40 @@ AES: require('./crypter/aes'),

/**
* Contains type constants that represent can be used to compose different crypto method, each crypto method consist one of:
*
* - Signer
* - Hahser
*
* @readonly
* @type {object}
* @name types
* @static
*/
types,
/**
* Get signer instance
*
* @function
* @param {number} type - algorithm used to derive key pair, possible values are
* - types.KeyType.ED25519
* - types.KeyType.SECP256k1
* @returns {object} signer instance
* @example
* const { Signer, getSigner, types } = require('@arcblock/mcrypto');
* const message = 'some message to sign';
*
* // Use Signer directly
* const keyPair = Signer.Ed25519.genKeyPair();
* const signature = Signer.Ed25519.sign(message, keyPair.secretKey);
* const result = Signer.Ed25519.verify(message, signature, keyPair.publicKey);
* assert.ok(result);
*
* // Get signer on fly
* const signer = getSigner(types.KeyType.ED25519);
* const keyPair1 = signer.genKeyPair();
* const signature1 = signer.sign(message, keyPair1.secretKey);
* const result1 = signer.verify(message, signature1, keyPair1.publicKey);
* assert.ok(result1);
*/
getSigner(type) {

@@ -60,2 +138,26 @@ if (typeof Signers[type] === 'undefined') {

},
/**
* Get hasher instance
*
* @function
* @param {number} type - algorithm used to hash data, possible values
* - types.HashType.KECCAK
* - types.HashType.KECCAK_384
* - types.HashType.KECCAK_512
* - types.HashType.SHA3
* - types.HashType.SHA3_384
* - types.HashType.SHA3_512
* @returns {object} hasher instance
* @example
* const { Hasher, getHasher, types } = require('@arcblock/mcrypto');
*
* // Choose from Hasher
* const message = 'message to hash';
* const hash = Hasher.SHA2.hash256(message);
*
* // user getHasher
* const hashFn = getHasher(types.HashType.SHA3);
* const hash2 = hashFn(message);
*/
getHasher(type) {

@@ -68,3 +170,3 @@ if (typeof Hashers[type] === 'undefined') {

},
};
});

@@ -84,3 +186,1 @@ const Signers = Object.freeze({

});
module.exports = Mcrypto;

@@ -7,2 +7,7 @@ const ed25519 = require('tweetnacl').sign;

/**
* Signer implementation for ed25519, based on `tweetnacl`
*
* @class Ed25519Signer
*/
class Ed25519Signer extends Signer {

@@ -25,2 +30,15 @@ constructor() {

/**
* @public
* @typedef KeyPair
* @prop {string} publicKey - publicKey in hex format
* @prop {string} secretKey - secretKey in hex format
* @memberof Ed25519Signer
*/
/**
* Generate random secret/public key pair
*
* @returns {KeyPair}
*/
genKeyPair() {

@@ -36,2 +54,8 @@ const seed = Uint8Array.from(randomBytes(32));

/**
* Get publicKey from secretKey
*
* @param {string|buffer} sk - can be either a hex encoded string or a buffer
* @returns {string} hex encoded publicKey
*/
getPublicKey(sk) {

@@ -43,2 +67,9 @@ const skBytes = this.toUint8Array(sk);

/**
* Sign a message and get the signature hex
*
* @param {string|buffer} message
* @param {string|buffer} sk
* @returns {string} hex encoded signature
*/
sign(message, sk) {

@@ -52,2 +83,10 @@ const skBytes = this.toUint8Array(sk);

/**
* Verify if a signature is valid
*
* @param {string|buffer} message
* @param {string|buffer} signature
* @param {string|buffer} pk
* @returns {bool}
*/
verify(message, signature, pk) {

@@ -54,0 +93,0 @@ const pkBytes = this.toUint8Array(pk);

@@ -11,2 +11,7 @@ const EC = require('elliptic').ec;

/**
* Signer implementation for secp256k1, based on `elliptic`
*
* @class Secp256k1Signer
*/
class Secp256k1Signer extends Signer {

@@ -26,2 +31,15 @@ constructor() {

/**
* @public
* @typedef KeyPair
* @prop {string} publicKey - publicKey in hex format
* @prop {string} secretKey - secretKey in hex format
* @memberof Secp256k1Signer
*/
/**
* Generate random secret/public key pair
*
* @returns {KeyPair}
*/
genKeyPair() {

@@ -36,2 +54,8 @@ let sk = null;

/**
* Get publicKey from secretKey
*
* @param {string} sk - must be a hex encoded string
* @returns {string} hex encoded publicKey
*/
getPublicKey(sk) {

@@ -42,2 +66,9 @@ const pk = secp256k1.keyFromPrivate(this.strip0x(sk), encoding).getPublic(compressed, encoding);

/**
* Sign a message and get the signature hex
*
* @param {string} message
* @param {string} sk
* @returns {string} hex encoded signature
*/
sign(message, sk) {

@@ -51,2 +82,10 @@ const signature = secp256k1

/**
* Verify if a signature is valid
*
* @param {string} message
* @param {string} signature
* @param {string} pk
* @returns {bool}
*/
verify(message, signature, pk) {

@@ -53,0 +92,0 @@ return secp256k1

6

package.json
{
"name": "@arcblock/mcrypto",
"version": "0.20.0",
"version": "0.20.5",
"description": "Crypto lib that provides signer,crypter,hasher interface",

@@ -40,3 +40,3 @@ "keywords": [

"dependencies": {
"@arcblock/forge-util": "^0.20.0",
"@arcblock/forge-util": "^0.20.5",
"crypto-js": "^3.1.9-1",

@@ -49,3 +49,3 @@ "elliptic": "^6.4.1",

},
"gitHead": "1bcb6daa62e48486496c3e73518c23ba27489284"
"gitHead": "b8526f942e8a04bcbeb47b0d6e1bd77570a29dd2"
}
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