4.1.1 • Published 3 days ago

@libp2p/crypto v4.1.1

Weekly downloads
-
License
Apache-2.0 OR MIT
Repository
github
Last release
3 days ago

@libp2p/crypto

libp2p.io Discuss codecov CI

Crypto primitives for libp2p

Table of contents

Install

$ npm i @libp2p/crypto

Browser <script> tag

Loading this module through a script tag will make it's exports available as Libp2pCrypto in the global namespace.

<script src="https://unpkg.com/@libp2p/crypto/dist/index.min.js"></script>

This repo contains the JavaScript implementation of the crypto primitives needed for libp2p. This is based on this go implementation.

Lead Maintainer

Jacob Heun

Usage

const crypto = require('libp2p-crypto')

// Now available to you:
//
// crypto.aes
// crypto.hmac
// crypto.keys
// etc.
//
// See full API details below...

Web Crypto API

The libp2p-crypto library depends on the Web Crypto API in the browser. Web Crypto is available in all modern browsers, however browsers restrict its usage to Secure Contexts.

This means you will not be able to use some libp2p-crypto functions in the browser when the page is served over HTTP. To enable the Web Crypto API and allow libp2p-crypto to work fully, please serve your page over HTTPS.

API

crypto.aes

Exposes an interface to AES encryption (formerly Rijndael), as defined in U.S. Federal Information Processing Standards Publication 197.

This uses CTR mode.

crypto.aes.create(key, iv)

  • key: Uint8Array The key, if length 16 then AES 128 is used. For length 32, AES 256 is used.
  • iv: Uint8Array Must have length 16.

Returns Promise<{decrypt<Function>, encrypt<Function>}>

decrypt(data)
  • data: Uint8Array

Returns Promise<Uint8Array>

encrypt(data)
  • data: Uint8Array

Returns Promise<Uint8Array>

const crypto = require('libp2p-crypto')

// Setting up Key and IV

// A 16 bytes array, 128 Bits, AES-128 is chosen
const key128 = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])

// A 16 bytes array, 128 Bits,
const IV = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])

async function main () {
  const decryptedMessage = 'Hello, world!'

  // Encrypting
  const cipher = await crypto.aes.create(key128, IV)
  const encryptedBuffer = await cipher.encrypt(Uint8Array.from(decryptedMessage))
  console.log(encryptedBuffer)
  // prints: <Uint8Array 42 f1 67 d9 2e 42 d0 32 9e b1 f8 3c>

  // Decrypting
  const decipher = await crypto.aes.create(key128, IV)
  const decryptedBuffer = await cipher.decrypt(encryptedBuffer)

  console.log(decryptedBuffer)
  // prints: <Uint8Array 42 f1 67 d9 2e 42 d0 32 9e b1 f8 3c>

  console.log(decryptedBuffer.toString('utf-8'))
  // prints: Hello, world!
}

main()

crypto.hmac

Exposes an interface to the Keyed-Hash Message Authentication Code (HMAC) as defined in U.S. Federal Information Processing Standards Publication 198. An HMAC is a cryptographic hash that uses a key to sign a message. The receiver verifies the hash by recomputing it using the same key.

crypto.hmac.create(hash, secret)

  • hash: String
  • secret: Uint8Array

Returns Promise<{digest<Function>}>

digest(data)
  • data: Uint8Array

Returns Promise<Uint8Array>

Example:

const crypto = require('libp2p-crypto')

async function main () {
  const hash = 'SHA1' // 'SHA256' || 'SHA512'
  const hmac = await crypto.hmac.create(hash, uint8ArrayFromString('secret'))
  const sig = await hmac.digest(uint8ArrayFromString('hello world'))
  console.log(sig)
}

main()

crypto.keys

Supported Key Types

The generateKeyPair, marshalPublicKey, and marshalPrivateKey functions accept a string type argument.

Currently the 'RSA', 'ed25519', and secp256k1 types are supported, although ed25519 and secp256k1 keys support only signing and verification of messages. For encryption / decryption support, RSA keys should be used.

crypto.keys.generateKeyPair(type, bits)

Returns Promise<{privateKey<Uint8Array>, publicKey<Uint8Array>}>

Generates a keypair of the given type and bitsize.

crypto.keys.generateEphemeralKeyPair(curve)

  • curve: String, one of 'P-256', 'P-384', 'P-521' is currently supported

Returns Promise

Generates an ephemeral public key and returns a function that will compute the shared secret key.

Focuses only on ECDH now, but can be made more general in the future.

Resolves to an object of the form:

{
  key: Uint8Array,
  genSharedKey: Function
}

crypto.keys.keyStretcher(cipherType, hashType, secret)

  • cipherType: String, one of 'AES-128', 'AES-256', 'Blowfish'
  • hashType: String, one of 'SHA1', SHA256, SHA512
  • secret: Uint8Array

Returns Promise

Generates a set of keys for each party by stretching the shared key.

Resolves to an object of the form:

{
  k1: {
    iv: Uint8Array,
    cipherKey: Uint8Array,
    macKey: Uint8Array
  },
  k2: {
    iv: Uint8Array,
    cipherKey: Uint8Array,
    macKey: Uint8Array
  }
}

crypto.keys.marshalPublicKey(key, [type])

  • key: keys.rsa.RsaPublicKey | keys.ed25519.Ed25519PublicKey | keys.secp256k1.Secp256k1PublicKey
  • type: String, see Supported Key Types above. Defaults to 'rsa'.

Returns Uint8Array

Converts a public key object into a protobuf serialized public key.

crypto.keys.unmarshalPublicKey(buf)

  • buf: Uint8Array

Returns RsaPublicKey|Ed25519PublicKey|Secp256k1PublicKey

Converts a protobuf serialized public key into its representative object.

crypto.keys.marshalPrivateKey(key, [type])

  • key: keys.rsa.RsaPrivateKey | keys.ed25519.Ed25519PrivateKey | keys.secp256k1.Secp256k1PrivateKey
  • type: String, see Supported Key Types above.

Returns Uint8Array

Converts a private key object into a protobuf serialized private key.

crypto.keys.unmarshalPrivateKey(buf)

  • buf: Uint8Array

Returns Promise<RsaPrivateKey|Ed25519PrivateKey|Secp256k1PrivateKey>

Converts a protobuf serialized private key into its representative object.

crypto.keys.import(encryptedKey, password)

  • encryptedKey: string
  • password: string

Returns Promise<PrivateKey>

Converts an exported private key into its representative object. Supported formats are 'pem' (RSA only) and 'libp2p-key'.

privateKey.export(password, format)

  • password: string
  • format: string the format to export to: 'pem' (rsa only), 'libp2p-key'

Returns string

Exports the password protected PrivateKey. RSA keys will be exported as password protected PEM by default. Ed25519 and Secp256k1 keys will be exported as password protected AES-GCM base64 encoded strings ('libp2p-key' format).

crypto.randomBytes(number)

  • number: Number

Returns Uint8Array

Generates a Uint8Array with length number populated by random bytes.

crypto.pbkdf2(password, salt, iterations, keySize, hash)

  • password: String
  • salt: String
  • iterations: Number
  • keySize: Number in bytes
  • hash: String the hashing algorithm ('sha1', 'sha2-512', ...)

Computes the Password Based Key Derivation Function 2; returning a new password.

Contribute

Feel free to join in. All welcome. Open an issue!

This repository falls under the IPFS Code of Conduct.

npm.io

API Docs

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

4.1.1-a11e135c2

3 days ago

4.1.1-9d13a2f6a

4 days ago

4.1.0-de3f7aeaf

4 days ago

4.1.1

4 days ago

4.1.0-3d7a9da17

5 days ago

4.1.0-c82432312

4 days ago

4.1.0-3bc94b403

5 days ago

4.1.0-998fcaf94

8 days ago

4.1.0-e1923b0a7

8 days ago

4.1.0-90cfd25e2

10 days ago

4.0.6-2281f802b

11 days ago

4.0.6-c2181f0cf

11 days ago

4.1.0

11 days ago

4.0.6-fd1f8343d

12 days ago

4.0.6-eaf8ac7cf

13 days ago

4.0.6-acef72613

13 days ago

4.0.6-ea4f26285

19 days ago

4.0.6-31c78f4ed

20 days ago

4.0.6-08dabd390

20 days ago

4.0.6

22 days ago

4.0.5-2c56203f9

23 days ago

4.0.5-b17824a1d

22 days ago

4.0.5-732c436d4

26 days ago

4.0.5-936dbba10

26 days ago

4.0.5-2b2958fe6

26 days ago

4.0.5

29 days ago

4.0.4-3e515f007

30 days ago

4.0.4-d446c6c31

1 month ago

4.0.4-a2b41f793

1 month ago

4.0.4-4fc0a7d30

1 month ago

4.0.4-3ffecc5bf

1 month ago

4.0.4-1f589c822

1 month ago

4.0.4-80278b36b

1 month ago

4.0.4-ab5f05763

1 month ago

4.0.4-afe15f669

1 month ago

4.0.3-82901e785

1 month ago

4.0.4

1 month ago

4.0.3-330a5ed72

1 month ago

4.0.3-59a97b61b

1 month ago

4.0.3-1fc929c1c

1 month ago

4.0.3-bf720c045

1 month ago

4.0.3-cad9cf007

2 months ago

4.0.3-fad3074b8

2 months ago

4.0.3-e1798aa26

2 months ago

4.0.3-f39ce5f13

2 months ago

4.0.3-83ef3717e

2 months ago

4.0.3-ab466004b

2 months ago

4.0.3-a9cc0ee49

2 months ago

4.0.3-2122a713d

2 months ago

4.0.3-28e51652a

2 months ago

4.0.3-f71bc49bd

2 months ago

4.0.3

2 months ago

4.0.2-f0d2b52d0

2 months ago

4.0.2-e1c01370b

2 months ago

4.0.2-8072a2e59

3 months ago

4.0.2-bedfd0aa2

3 months ago

4.0.2-b1b77adb4

3 months ago

4.0.2-fb7c51c3c

3 months ago

4.0.2

3 months ago

4.0.1-2370d1c39

3 months ago

4.0.1-9891ecd73

3 months ago

4.0.1-03ff9fd82

3 months ago

4.0.1-1cb2408ac

3 months ago

4.0.1-0321812e7

3 months ago

4.0.2-0c7bbbb07

3 months ago

4.0.1-dab5cf724

3 months ago

4.0.1-e1db332a4

3 months ago

4.0.1-6f323de7d

3 months ago

4.0.1-c9ed1c7d6

3 months ago

4.0.1-f4dda4a3c

3 months ago

4.0.1-2e464c099

3 months ago

4.0.1-72f0e09f7

3 months ago

4.0.1-dbc92ab74

3 months ago

4.0.1-74fb5671d

3 months ago

4.0.1-74477f6ea

4 months ago

4.0.1-9376e61a1

4 months ago

4.0.1-8c6654c3a

4 months ago

4.0.1-08f6f607d

4 months ago

4.0.1-3e47d88fd

4 months ago

4.0.1-f27138ca1

4 months ago

4.0.1

4 months ago

4.0.0-092861e23

4 months ago

4.0.0

4 months ago

4.0.0-8bbd43628

4 months ago

3.0.4-ddaa59a60

4 months ago

3.0.4-856ccd708

4 months ago

3.0.4-4691f4173

4 months ago

3.0.4-ee7ffe9b9

4 months ago

3.0.4-900236724

4 months ago

3.0.4-821a38e24

4 months ago

3.0.4

4 months ago

3.0.3-444d83751

4 months ago

3.0.3-581574d6d

4 months ago

3.0.3-d011f6130

4 months ago

3.0.3-528d73781

4 months ago

3.0.3-ba7089984

4 months ago

3.0.3-388d02b33

4 months ago

3.0.3

4 months ago

3.0.2-5d1f68e92

4 months ago

3.0.2-28587d24f

4 months ago

3.0.2-4e0135c7d

4 months ago

3.0.2-c00378909

5 months ago

3.0.2-cd8cafcd5

5 months ago

3.0.2-178fe2671

5 months ago

3.0.2-6fd681d09

5 months ago

3.0.2-83dfc7dc8

5 months ago

3.0.2-ad6f70bf3

5 months ago

3.0.2-f71f2e14e

5 months ago

3.0.2-984f13e42

5 months ago

3.0.2-07f3afe2d

5 months ago

3.0.2-230afea4b

5 months ago

3.0.2-9eff7eff0

5 months ago

3.0.2-a7c6a93c6

5 months ago

3.0.2-f81be145a

5 months ago

3.0.2-01e9a5fe4

5 months ago

3.0.2

5 months ago

3.0.2-742915567

5 months ago

3.0.1-341581166

5 months ago

3.0.1-6d11e8268

5 months ago

3.0.1-d10506189

5 months ago

3.0.1-64a915ae9

5 months ago

3.0.1-3bf6387ff

5 months ago

3.0.1-93890c8f9

5 months ago

3.0.1-887c6ffe1

5 months ago

3.0.1-16588d27c

5 months ago

1.0.17-5315f7bc

10 months ago

2.0.4-7d8b1551

7 months ago

2.0.3-725f5df1

9 months ago

2.0.0-72e81dc1

9 months ago

2.0.8-0f5c305af

6 months ago

3.0.1-bca8d6e68

5 months ago

3.0.1-561797a89

5 months ago

2.0.4-28794fe4

7 months ago

1.0.17-c2232166

9 months ago

2.0.7-70d5efc2e

6 months ago

1.0.17-eabf6f36

9 months ago

2.0.2-18567b7c

9 months ago

2.0.1-446fff87

9 months ago

2.0.3-32825633

8 months ago

2.0.3-e664d14f

9 months ago

2.0.5-f9d1c072

6 months ago

2.0.3-0d228f9f

8 months ago

2.0.4-6640116d

8 months ago

2.0.2-5e85154b

9 months ago

3.0.1-5a9362e21

5 months ago

3.0.0-06e6d235f

5 months ago

2.0.3-0634e3b7

8 months ago

2.0.7-78db573f9

6 months ago

2.0.8-05b52d69c

6 months ago

2.0.4-5a6a4379

7 months ago

2.0.0-58421e11

9 months ago

2.0.5-ab2c1f67

6 months ago

2.0.0-e66f4891

9 months ago

2.0.3-9a69e6f7

8 months ago

2.0.8-74e84bc29

5 months ago

2.0.0-8f855a3c

9 months ago

2.0.3-0ee4f784

8 months ago

2.0.8-97ab31c0c

6 months ago

2.0.6-7903d7a5

6 months ago

2.0.4-f09ac4a7

7 months ago

2.0.3-24c1c248

8 months ago

3.0.0-8e4fbe13a

5 months ago

2.0.3-f3fd7b62

9 months ago

2.0.3-24a5edae

8 months ago

2.0.2-123ded59

9 months ago

2.0.0-5ffa7a74

9 months ago

2.0.2-a31b420f

9 months ago

2.0.3-d9948596

9 months ago

2.0.2-6b839807

9 months ago

2.0.6-d5ef1c91

6 months ago

1.0.17-57c32721

9 months ago

3.0.1-53224004f

5 months ago

3.0.0-551622a96

5 months ago

2.0.4-f1053159

7 months ago

2.0.4-b57bca44

7 months ago

2.0.7-f4fac961

6 months ago

3.0.1-10ea19700

5 months ago

1.0.17-5eee70a4

10 months ago

2.0.3-4559a624

8 months ago

1.0.17-1f7e18b0

9 months ago

2.0.3-d9159dd5

8 months ago

3.0.1

5 months ago

2.0.2-9c0353cf

9 months ago

1.0.17-7debe031

9 months ago

1.0.17-562f9b08

10 months ago

2.0.8-6625a27fc

6 months ago

2.0.2-02b89323

9 months ago

2.0.4-ae36e86b

7 months ago

3.0.0

5 months ago

2.0.6-69581367

6 months ago

1.0.17-a41d25d4

9 months ago

2.0.7-dfbe0cc0

6 months ago

2.0.2-4c1a33b3

9 months ago

3.0.1-bcfa15993

5 months ago

3.0.1-e7167fe52

5 months ago

2.0.3-73b87c5a

8 months ago

2.0.3-4ef9c79c

9 months ago

2.0.4-96166ada

7 months ago

2.0.3-6abcd22f

9 months ago

2.0.8-3dee5df4d

6 months ago

2.0.3-28d6722f

8 months ago

2.0.3-098ba082

8 months ago

2.0.7-1d141331a

6 months ago

2.0.3-10cbc8fa

8 months ago

1.0.17-7f60b579

9 months ago

2.0.3-01acccef

8 months ago

3.0.1-7861ed882

5 months ago

2.0.4-6cb80f7d

8 months ago

2.0.3-0ce318ec

8 months ago

2.0.7-fb8a6f188

6 months ago

3.0.0-bcf18265e

5 months ago

2.0.4-c97dea04

7 months ago

2.0.3-c4eff4c5

8 months ago

2.0.5-cf3ae893

7 months ago

2.0.6-50442d7a

6 months ago

2.0.3-63041afe

8 months ago

2.0.3-122f1e67

8 months ago

2.0.3-d30f09f2

9 months ago

3.0.1-8c169db1b

5 months ago

2.0.3-4db2f5f5

8 months ago

3.0.1-cf963694f

5 months ago

2.0.3

9 months ago

2.0.2

9 months ago

2.0.8-9c67c5b3d

6 months ago

2.0.6-16a87076

6 months ago

2.0.5

7 months ago

2.0.4

8 months ago

2.0.7

6 months ago

2.0.6

6 months ago

2.0.8

6 months ago

2.0.7-d25d9510

6 months ago

2.0.1

9 months ago

2.0.3-87165551

9 months ago

2.0.0

9 months ago

2.0.4-e3ab1929

7 months ago

1.0.17-69c93ac5

9 months ago

1.0.17-e9cafd3d

10 months ago

2.0.3-5294f14c

8 months ago

2.0.5-68504939

6 months ago

2.0.7-fdcb801e

6 months ago

1.0.17-791f56f0

9 months ago

2.0.6-025c082a

6 months ago

2.0.8-effcfaa8e

6 months ago

2.0.4-72319fe6

7 months ago

2.0.0-ef83dd1d

9 months ago

2.0.5-b5a808af

6 months ago

2.0.3-a533cc39

8 months ago

2.0.5-77e3cbc3

7 months ago

2.0.3-88c47f51

8 months ago

2.0.8-d8f5bc211

6 months ago

2.0.3-2b755a82

8 months ago

2.0.3-20d5f220

8 months ago

2.0.8-d729d66a5

5 months ago

2.0.8-bb6ceb192

6 months ago

1.0.17-6eab9c5e

10 months ago

2.0.8-7877a50e0

5 months ago

2.0.8-c960eb659

6 months ago

2.0.0-8d49602f

9 months ago

2.0.0-8f681db3

9 months ago

2.0.8-e2267d437

5 months ago

2.0.8-13a870cbe

6 months ago

2.0.4-980857c3

7 months ago

2.0.3-7517082d

8 months ago

1.0.17-a1ec46b5

9 months ago

2.0.6-346ff5a2

6 months ago

2.0.3-89778624

8 months ago

2.0.8-6b6ba9ab7

5 months ago

2.0.3-6a02d765

8 months ago

2.0.3-13f5b48e

8 months ago

2.0.8-0b4a2ee79

5 months ago

1.0.17-06f4901a

10 months ago

2.0.4-91842c93

7 months ago

2.0.3-87dc7e9f

9 months ago

2.0.3-46dc3ce9

8 months ago

1.0.17-c999d6a7

9 months ago

2.0.8-8f921ee97

5 months ago

1.0.17-7b5c54dd

10 months ago

2.0.2-32212959

9 months ago

3.0.1-738dd40f1

5 months ago

2.0.3-b599905c

9 months ago

3.0.0-7682861f9

5 months ago

3.0.1-9197f10ba

5 months ago

2.0.5-7534ae7b

6 months ago

2.0.8-adea7bbbf

6 months ago

2.0.4-f670307a

7 months ago

2.0.2-364e0592

9 months ago

1.0.17-b36ec7f2

10 months ago

2.0.6-b686fb5a

6 months ago

1.0.17-b1024c6c

10 months ago

2.0.3-a6be8f0f

8 months ago

2.0.3-7b2ddc17

9 months ago

2.0.2-a1fbb7e2

9 months ago

2.0.5-50f912c2

6 months ago

2.0.4-e9099d40

7 months ago

2.0.2-3345f28b

9 months ago

2.0.8-68db79f6b

5 months ago

3.0.1-09dd02987

5 months ago

3.0.0-273d8177c

5 months ago

2.0.8-8bb6d5333

5 months ago

2.0.2-eaac8943

9 months ago

2.0.8-4a474d54d

5 months ago

2.0.2-e26848b0

9 months ago

3.0.1-f537b3731

5 months ago

2.0.7-8b82e68e8

6 months ago

3.0.0-a32e70bac

5 months ago

2.0.0-a4a10fd4

9 months ago

2.0.4-c88de8e1

7 months ago

2.0.8-9ad8f8686

6 months ago

1.0.17-daeb43d8

10 months ago

2.0.5-62a56b54

7 months ago

2.0.4-e8123d3f

7 months ago

2.0.7-051154dd

6 months ago

2.0.0-c858ca7f

9 months ago

2.0.0-fdd80820

9 months ago

2.0.4-972b10a9

8 months ago

3.0.1-6c1f0ee81

5 months ago

1.0.17-879f4794

10 months ago

1.0.17-85a317bb

10 months ago

1.0.17-f427cfc9

10 months ago

1.0.17-2e561fe9

10 months ago

1.0.17-7fb23cd3

10 months ago

1.0.17-42c1c097

10 months ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.17-ab0e3980

11 months ago

1.0.17-05abd49f

11 months ago

1.0.17-d853d124

11 months ago

1.0.17-6fdaa7dc

11 months ago

1.0.17-8b0e6bef

11 months ago

1.0.17-3dfc236e

11 months ago

1.0.17-ea8a0637

11 months ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

1.0.3

2 years ago

0.22.12

2 years ago

0.22.14

2 years ago

0.22.13

2 years ago

0.22.11

2 years ago

0.22.10

2 years ago

0.22.9

2 years ago

0.22.8

2 years ago

0.22.7

2 years ago

0.22.6

2 years ago

0.22.5

2 years ago

0.22.4

2 years ago

0.22.3

2 years ago

0.22.2

2 years ago

0.22.1

2 years ago

0.22.0

2 years ago

0.0.0

2 years ago