Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@libp2p/crypto

Package Overview
Dependencies
Maintainers
6
Versions
574
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@libp2p/crypto - npm Package Compare versions

Comparing version 2.0.2 to 2.0.3

6

dist/src/keys/ed25519-browser.js

@@ -1,2 +0,2 @@

import * as ed from '@noble/ed25519';
import { ed25519 as ed } from '@noble/curves/ed25519';
const PUBLIC_KEY_BYTE_LENGTH = 32;

@@ -10,3 +10,3 @@ const PRIVATE_KEY_BYTE_LENGTH = 64; // private key is actually 32 bytes but for historical reasons we concat private and public keys

const privateKeyRaw = ed.utils.randomPrivateKey();
const publicKey = await ed.getPublicKey(privateKeyRaw);
const publicKey = ed.getPublicKey(privateKeyRaw);
// concatenated the public key to the private key

@@ -31,3 +31,3 @@ const privateKey = concatKeys(privateKeyRaw, publicKey);

const privateKeyRaw = seed;
const publicKey = await ed.getPublicKey(privateKeyRaw);
const publicKey = ed.getPublicKey(privateKeyRaw);
const privateKey = concatKeys(privateKeyRaw, publicKey);

@@ -34,0 +34,0 @@ return {

import { CodeError } from '@libp2p/interface/errors';
import * as secp from '@noble/secp256k1';
import { secp256k1 as secp } from '@noble/curves/secp256k1';
import { sha256 } from 'multiformats/hashes/sha2';

@@ -15,3 +15,4 @@ const PRIVATE_KEY_BYTE_LENGTH = 32;

try {
return await secp.sign(digest, key);
const signature = secp.sign(digest, key);
return signature.toDERRawBytes();
}

@@ -35,7 +36,7 @@ catch (err) {

export function compressPublicKey(key) {
const point = secp.Point.fromHex(key).toRawBytes(true);
const point = secp.ProjectivePoint.fromHex(key).toRawBytes(true);
return point;
}
export function decompressPublicKey(key) {
const point = secp.Point.fromHex(key).toRawBytes(false);
const point = secp.ProjectivePoint.fromHex(key).toRawBytes(false);
return point;

@@ -53,3 +54,3 @@ }

try {
secp.Point.fromHex(key);
secp.ProjectivePoint.fromHex(key);
}

@@ -56,0 +57,0 @@ catch (err) {

import { CodeError } from '@libp2p/interface/errors';
import { utils } from '@noble/secp256k1';
import { randomBytes as randB } from '@noble/hashes/utils';
export default function randomBytes(length) {

@@ -7,4 +7,4 @@ if (isNaN(length) || length <= 0) {

}
return utils.randomBytes(length);
return randB(length);
}
//# sourceMappingURL=random-bytes.js.map
{
"name": "@libp2p/crypto",
"version": "2.0.2",
"version": "2.0.3",
"description": "Crypto primitives for libp2p",

@@ -88,5 +88,5 @@ "license": "Apache-2.0 OR MIT",

"dependencies": {
"@libp2p/interface": "^0.1.1",
"@noble/ed25519": "^1.6.0",
"@noble/secp256k1": "^1.5.4",
"@libp2p/interface": "^0.1.2",
"@noble/curves": "^1.1.0",
"@noble/hashes": "^1.3.1",
"multiformats": "^12.0.1",

@@ -96,7 +96,7 @@ "node-forge": "^1.1.0",

"uint8arraylist": "^2.4.3",
"uint8arrays": "^4.0.4"
"uint8arrays": "^4.0.6"
},
"devDependencies": {
"@types/mocha": "^10.0.0",
"aegir": "^40.0.1",
"aegir": "^40.0.8",
"benchmark": "^2.1.4",

@@ -103,0 +103,0 @@ "protons": "^7.0.2"

@@ -9,3 +9,3 @@ import { cipherMode } from './cipher-mode.js'

export async function create (key: Uint8Array, iv: Uint8Array): Promise<AESCipher> { // eslint-disable-line require-await
export async function create (key: Uint8Array, iv: Uint8Array): Promise<AESCipher> {
const mode = cipherMode(key)

@@ -16,7 +16,7 @@ const cipher = ciphers.createCipheriv(mode, key, iv)

const res: AESCipher = {
async encrypt (data) { // eslint-disable-line require-await
async encrypt (data) {
return cipher.update(data)
},
async decrypt (data) { // eslint-disable-line require-await
async decrypt (data) {
return decipher.update(data)

@@ -23,0 +23,0 @@ }

@@ -35,3 +35,3 @@ import { concat } from 'uint8arrays/concat'

*/
async function encrypt (data: Uint8Array, password: string | Uint8Array): Promise<Uint8Array> { // eslint-disable-line require-await
async function encrypt (data: Uint8Array, password: string | Uint8Array): Promise<Uint8Array> {
const salt = crypto.getRandomValues(new Uint8Array(saltLength))

@@ -38,0 +38,0 @@ const nonce = crypto.getRandomValues(new Uint8Array(nonceLength))

@@ -17,3 +17,3 @@ import crypto from 'crypto'

async function encryptWithKey (data: Uint8Array, key: Uint8Array): Promise<Uint8Array> { // eslint-disable-line require-await
async function encryptWithKey (data: Uint8Array, key: Uint8Array): Promise<Uint8Array> {
const nonce = crypto.randomBytes(nonceLength)

@@ -35,3 +35,3 @@

*/
async function encrypt (data: Uint8Array, password: string | Uint8Array): Promise<Uint8Array> { // eslint-disable-line require-await
async function encrypt (data: Uint8Array, password: string | Uint8Array): Promise<Uint8Array> {
// Generate a 128-bit salt using a CSPRNG.

@@ -58,3 +58,3 @@ const salt = crypto.randomBytes(saltLength)

*/
async function decryptWithKey (ciphertextAndNonce: Uint8Array, key: Uint8Array): Promise<Uint8Array> { // eslint-disable-line require-await
async function decryptWithKey (ciphertextAndNonce: Uint8Array, key: Uint8Array): Promise<Uint8Array> {
// Create Uint8Arrays of nonce, ciphertext and tag.

@@ -83,3 +83,3 @@ const nonce = ciphertextAndNonce.subarray(0, nonceLength)

*/
async function decrypt (data: Uint8Array, password: string | Uint8Array): Promise<Uint8Array> { // eslint-disable-line require-await
async function decrypt (data: Uint8Array, password: string | Uint8Array): Promise<Uint8Array> {
// Create Uint8Arrays of salt and ciphertextAndNonce.

@@ -86,0 +86,0 @@ const salt = data.subarray(0, saltLength)

@@ -30,3 +30,3 @@ import webcrypto from '../webcrypto.js'

return {
async digest (data: Uint8Array) { // eslint-disable-line require-await
async digest (data: Uint8Array) {
return sign(key, data)

@@ -33,0 +33,0 @@ },

@@ -11,3 +11,3 @@ import crypto from 'crypto'

const res = {
async digest (data: Uint8Array) { // eslint-disable-line require-await
async digest (data: Uint8Array) {
const hmac = crypto.createHmac(hash.toLowerCase(), secret)

@@ -14,0 +14,0 @@ hmac.update(data)

@@ -14,3 +14,3 @@ import crypto from 'crypto'

export async function generateEphmeralKeyPair (curve: string): Promise<ECDHKey> { // eslint-disable-line require-await
export async function generateEphmeralKeyPair (curve: string): Promise<ECDHKey> {
if (curve !== 'P-256' && curve !== 'P-384' && curve !== 'P-521') {

@@ -26,3 +26,3 @@ throw new CodeError(`Unknown curve: ${curve}. Must be ${names}`, 'ERR_INVALID_CURVE')

async genSharedKey (theirPub: Uint8Array, forcePrivate?: ECDHKeyPair): Promise<Uint8Array> { // eslint-disable-line require-await
async genSharedKey (theirPub: Uint8Array, forcePrivate?: ECDHKeyPair): Promise<Uint8Array> {
if (forcePrivate != null) {

@@ -29,0 +29,0 @@ ecdh.setPrivateKey(forcePrivate.private)

@@ -1,2 +0,2 @@

import * as ed from '@noble/ed25519'
import { ed25519 as ed } from '@noble/curves/ed25519'
import type { Uint8ArrayKeyPair } from './interface'

@@ -14,3 +14,3 @@

const privateKeyRaw = ed.utils.randomPrivateKey()
const publicKey = await ed.getPublicKey(privateKeyRaw)
const publicKey = ed.getPublicKey(privateKeyRaw)

@@ -38,3 +38,3 @@ // concatenated the public key to the private key

const privateKeyRaw = seed
const publicKey = await ed.getPublicKey(privateKeyRaw)
const publicKey = ed.getPublicKey(privateKeyRaw)

@@ -41,0 +41,0 @@ const privateKey = concatKeys(privateKeyRaw, publicKey)

@@ -18,3 +18,3 @@ import { CodeError } from '@libp2p/interface/errors'

async verify (data: Uint8Array, sig: Uint8Array): Promise<boolean> { // eslint-disable-line require-await
async verify (data: Uint8Array, sig: Uint8Array): Promise<boolean> {
return crypto.hashAndVerify(this._key, sig, data)

@@ -56,3 +56,3 @@ }

async sign (message: Uint8Array): Promise<Uint8Array> { // eslint-disable-line require-await
async sign (message: Uint8Array): Promise<Uint8Array> {
return crypto.hashAndSign(this._key, message)

@@ -59,0 +59,0 @@ }

@@ -44,3 +44,3 @@ import 'node-forge/lib/asn1.js'

// Generates a keypair of the given type and bitsize
export async function generateKeyPair (type: KeyTypes, bits?: number): Promise<PrivateKey> { // eslint-disable-line require-await
export async function generateKeyPair (type: KeyTypes, bits?: number): Promise<PrivateKey> {
return typeToKey(type).generateKeyPair(bits ?? 2048)

@@ -51,3 +51,3 @@ }

// seed is a 32 byte uint8array
export async function generateKeyPairFromSeed (type: KeyTypes, seed: Uint8Array, bits?: number): Promise<PrivateKey> { // eslint-disable-line require-await
export async function generateKeyPairFromSeed (type: KeyTypes, seed: Uint8Array, bits?: number): Promise<PrivateKey> {
if (type.toLowerCase() !== 'ed25519') {

@@ -87,3 +87,3 @@ throw new CodeError('Seed key derivation is unimplemented for RSA or secp256k1', 'ERR_UNSUPPORTED_KEY_DERIVATION_TYPE')

// representative object
export async function unmarshalPrivateKey (buf: Uint8Array): Promise<PrivateKey> { // eslint-disable-line require-await
export async function unmarshalPrivateKey (buf: Uint8Array): Promise<PrivateKey> {
const decoded = keysPBM.PrivateKey.decode(buf)

@@ -116,3 +116,3 @@ const data = decoded.Data ?? new Uint8Array()

*/
export async function importKey (encryptedKey: string, password: string): Promise<PrivateKey> { // eslint-disable-line require-await
export async function importKey (encryptedKey: string, password: string): Promise<PrivateKey> {
try {

@@ -119,0 +119,0 @@ const key = await importer(encryptedKey, password)

@@ -22,3 +22,3 @@ import { CodeError } from '@libp2p/interface/errors'

async verify (data: Uint8Array, sig: Uint8Array): Promise<boolean> { // eslint-disable-line require-await
async verify (data: Uint8Array, sig: Uint8Array): Promise<boolean> {
return crypto.hashAndVerify(this._key, sig, data)

@@ -66,3 +66,3 @@ }

async sign (message: Uint8Array): Promise<Uint8Array> { // eslint-disable-line require-await
async sign (message: Uint8Array): Promise<Uint8Array> {
return crypto.hashAndSign(this._key, message)

@@ -119,3 +119,3 @@ }

*/
async export (password: string, format = 'pkcs-8'): Promise<Multibase<'m'>> { // eslint-disable-line require-await
async export (password: string, format = 'pkcs-8'): Promise<Multibase<'m'>> {
if (format === 'pkcs-8') {

@@ -122,0 +122,0 @@ const buffer = new forge.util.ByteBuffer(this.marshal())

@@ -12,3 +12,3 @@ import crypto from 'crypto'

export async function generateKey (bits: number): Promise<JWKKeyPair> { // eslint-disable-line require-await
export async function generateKey (bits: number): Promise<JWKKeyPair> {
// @ts-expect-error node types are missing jwk as a format

@@ -30,3 +30,3 @@ const key = await keypair('rsa', {

// Takes a jwk key
export async function unmarshalPrivateKey (key: JsonWebKey): Promise<JWKKeyPair> { // eslint-disable-line require-await
export async function unmarshalPrivateKey (key: JsonWebKey): Promise<JWKKeyPair> {
if (key == null) {

@@ -54,3 +54,3 @@ throw new CodeError('Missing key parameter', 'ERR_MISSING_KEY')

export async function hashAndVerify (key: JsonWebKey, sig: Uint8Array, msg: Uint8Array): Promise<boolean> { // eslint-disable-line require-await
export async function hashAndVerify (key: JsonWebKey, sig: Uint8Array, msg: Uint8Array): Promise<boolean> {
return crypto.createVerify('RSA-SHA256')

@@ -57,0 +57,0 @@ .update(msg)

import { CodeError } from '@libp2p/interface/errors'
import * as secp from '@noble/secp256k1'
import { secp256k1 as secp } from '@noble/curves/secp256k1'
import { sha256 } from 'multiformats/hashes/sha2'

@@ -19,3 +19,4 @@

try {
return await secp.sign(digest, key)
const signature = secp.sign(digest, key)
return signature.toDERRawBytes()
} catch (err) {

@@ -39,3 +40,3 @@ throw new CodeError(String(err), 'ERR_INVALID_INPUT')

export function compressPublicKey (key: Uint8Array): Uint8Array {
const point = secp.Point.fromHex(key).toRawBytes(true)
const point = secp.ProjectivePoint.fromHex(key).toRawBytes(true)
return point

@@ -45,3 +46,3 @@ }

export function decompressPublicKey (key: Uint8Array): Uint8Array {
const point = secp.Point.fromHex(key).toRawBytes(false)
const point = secp.ProjectivePoint.fromHex(key).toRawBytes(false)
return point

@@ -60,3 +61,3 @@ }

try {
secp.Point.fromHex(key)
secp.ProjectivePoint.fromHex(key)
} catch (err) {

@@ -63,0 +64,0 @@ throw new CodeError(String(err), 'ERR_INVALID_PUBLIC_KEY')

import { CodeError } from '@libp2p/interface/errors'
import { utils } from '@noble/secp256k1'
import { randomBytes as randB } from '@noble/hashes/utils'

@@ -8,3 +8,3 @@ export default function randomBytes (length: number): Uint8Array {

}
return utils.randomBytes(length)
return randB(length)
}

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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