Package Exports
- @polkadot/util-crypto
- @polkadot/util-crypto/blake2/asU8a
- @polkadot/util-crypto/index
- @polkadot/util-crypto/keccak/asU8a
- @polkadot/util-crypto/mnemonic/generate
- @polkadot/util-crypto/mnemonic/toSecret
- @polkadot/util-crypto/mnemonic/toSeed
- @polkadot/util-crypto/mnemonic/validate
- @polkadot/util-crypto/nacl
- @polkadot/util-crypto/nacl/decrypt
- @polkadot/util-crypto/nacl/encrypt
- @polkadot/util-crypto/nacl/keypair/fromSecret
- @polkadot/util-crypto/nacl/keypair/fromSeed
- @polkadot/util-crypto/nacl/sign
- @polkadot/util-crypto/nacl/verify
- @polkadot/util-crypto/random/asU8a
- @polkadot/util-crypto/xxhash/asU8a
- @polkadot/util-crypto/xxhash/xxhash64/asHex
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@polkadot/util-crypto) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@polkadot/util-crypto
Various useful cyrpto utility functions that are used across all projects in the @polkadot namespace. It provides utility functions with additional safety checks, allowing not only for consistent coding, but also reducing the general boilerplate.
Usage
Installation -
yarn add @polkadot/util-crypto
Functions can be imported as follows:
import { mnemonicGenerate } from '@polkadot/util-crypto';
Documentation and Available Utilities
Below is a list of currently exposed methods published at the Polkadot-JS Common Documentation Portal.
- util-crypto
- Functions
- Blake2b
- Keccak
- Mnemonic
- Nacl
- Random
- SHA-512
- xxHash
- Functions
Blake2b
- Creates a blake2b u8a output containing a hash from a u8a input bytes using given bit-length or default bit-length of 256. An optional key u8a of up to 64 bytes may be provided
- Creates a blake2b hex string with optionally specified bit-length from the u8a input or default bit-length used is 256
- Uses the Blake2b hash function of blakejs library
- Note: All hashes used in Polkadot-JS are 32 bytes (i.e. 256 / 8)
- Note: BLAKE hash functions are in the Nacl library
- Note: Only handles up to 2**53 bytes of input
Keccak
- Creates a Keccak-256 hex string from the input (such as for conversion from seed phrase)
- Creates a Keccak-256 Uint8Array from the input
- Uses the Keccak-256 hash function from the js-sha3 library
Mnemonic
- Generate valid mnemonics (for account seed phrase) using BIP39
- Converts mnemonics to a valid seed secret with Uint8Array output
Nacl
- Implements NaCl secret-key authenticated encryption, public-key authenticated encryption, hashing, and public-key signatures
- Decrypts a message that has been encrypted with a secret key using Nacl
- Encrypts a message with a secret key and generates an associated nonce using Nacl
- Signing a message with a secret key to generate a valid signature using Nacl
- Verifies a signed message by providing the message, signature, and public key (associated with the secret key that signed it) using Nacl
Random
- Returns a sequence of secure random bytes in a variety of formats
- Creates a u8a with a specified (optional) bit-length (default 32) filled with random bytes
- Creates a hex string with a specified (optional) bit-length (default 32) filled with random bytes
- Creates a random number from random secure bytes
SHA-512
- Implements SHA-512 hashing functions for a variety of input and outputs
- Creates a SHA-512 hash of u8a input
- Uses the SHA-512 hash function of tweetnacl library
xxHash
- Create xxhash64 values with specified bitlengths
- Creates a xxhash64 u8a from the input with a specified (optional) bit-length (default 64)
- Creates a xxhash64 hex string from the input with a specified (optional) bit-length (default 64)
- Uses xxHash to generate values as BN, hex & number output