Module crypto
Crypto functions.
generate_random_bytes – Generates random byte array of the specified length and returns it in
base64
formatverify_signature – Verifies signed data using the provided public key. Raises error if verification is failed.
nacl_box_open – Decrypt and verify the cipher text using the receivers secret key, the senders public key, and the nonce.
hdkey_xprv_from_mnemonic – Generates an extended master private key that will be the root for all the derived keys
hdkey_derive_from_xprv – Returns extended private key derived from the specified extended private key and child index
get_crypto_box_info – Get Crypto Box Info. Used to get
encrypted_secret
that should be used for all the cryptobox initializations except the first one.clear_crypto_box_secret_cache – Removes cached secrets (overwrites with zeroes) from all signing and encryption boxes, derived from crypto box.
CryptoBoxSecretRandomSeedPhraseVariant – Creates Crypto Box from a random seed phrase. This option can be used if a developer doesn't want the seed phrase to leave the core library's memory, where it is stored encrypted.
CryptoBoxSecretPredefinedSeedPhraseVariant – Restores crypto box instance from an existing seed phrase. This type should be used when Crypto Box is initialized from a seed phrase, entered by a user.
CryptoBoxSecretEncryptedSecretVariant – Use this type for wallet reinitializations, when you already have
encrypted_secret
on hands. To get encrypted_secret
, use get_crypto_box_info
function after you initialized your crypto box for the first time.ParamsOfAppPasswordProvider – Interface that provides a callback that returns an encrypted password, used for cryptobox secret encryption
AppPasswordProvider – Interface that provides a callback that returns an encrypted password, used for cryptobox secret encryption
Integer factorization
Performs prime factorization – decomposition of a composite number into a product of smaller prime integers (factors). See [https://en.wikipedia.org/wiki/Integer_factorization]
type ParamsOfFactorize = {
composite: string
}
type ResultOfFactorize = {
factors: string[]
}
function factorize(
params: ParamsOfFactorize,
): Promise<ResultOfFactorize>;
function factorize_sync(
params: ParamsOfFactorize,
): ResultOfFactorize;
NOTE: Sync version is available only for
lib-node
binding.composite
: string – Hexadecimal representation of u64 composite number.
factors
: string[] – Two factors of composite or empty if composite can't be factorized.
Modular exponentiation
Performs modular exponentiation for big integers (
base
^exponent
mod modulus
). See [https://en.wikipedia.org/wiki/Modular_exponentiation]type ParamsOfModularPower = {
base: string,
exponent: string,
modulus: string
}
type ResultOfModularPower = {
modular_power: string
}
function modular_power(
params: ParamsOfModularPower,
): Promise<ResultOfModularPower>;
function modular_power_sync(
params: ParamsOfModularPower,
): ResultOfModularPower;
NOTE: Sync version is available only for
lib-node
binding.base
: string –base
argument of calculation.exponent
: string –exponent
argument of calculation.modulus
: string –modulus
argument of calculation.
modular_power
: string – Result of modular exponentiation
Calculates CRC16 using TON algorithm.
type ParamsOfTonCrc16 = {
data: string
}
type ResultOfTonCrc16 = {
crc: number
}
function ton_crc16(
params: ParamsOfTonCrc16,
): Promise<ResultOfTonCrc16>;
function ton_crc16_sync(
params: ParamsOfTonCrc16,
): ResultOfTonCrc16;
NOTE: Sync version is available only for
lib-node
binding.data
: string – Input data for CRC calculation. Encoded withbase64
.
crc
: number – Calculated CRC for input data.
Generates random byte array of the specified length and returns it in
base64
formattype ParamsOfGenerateRandomBytes = {
length: number
}
type ResultOfGenerateRandomBytes = {
bytes: string
}
function generate_random_bytes(
params: ParamsOfGenerateRandomBytes,
): Promise<ResultOfGenerateRandomBytes>;
function generate_random_bytes_sync(
params: ParamsOfGenerateRandomBytes,
): ResultOfGenerateRandomBytes;
NOTE: Sync version is available only for
lib-node
binding.length
: number – Size of random byte array.
bytes
: string – Generated bytes encoded inbase64
.
Converts public key to ton safe_format
type ParamsOfConvertPublicKeyToTonSafeFormat = {
public_key: string
}
type ResultOfConvertPublicKeyToTonSafeFormat = {
ton_public_key: string
}
function convert_public_key_to_ton_safe_format(
params: ParamsOfConvertPublicKeyToTonSafeFormat,
): Promise<ResultOfConvertPublicKeyToTonSafeFormat>;
function convert_public_key_to_ton_safe_format_sync(
params: ParamsOfConvertPublicKeyToTonSafeFormat,
): ResultOfConvertPublicKeyToTonSafeFormat;
NOTE: Sync version is available only for
lib-node
binding.public_key
: string – Public key - 64 symbols hex string
ton_public_key
: string – Public key represented in TON safe format.
Generates random ed25519 key pair.
type KeyPair = {
public: string,
secret: string
}
function generate_random_sign_keys(): Promise<KeyPair>;
function generate_random_sign_keys_sync(): KeyPair;
NOTE: Sync version is available only for
lib-node
binding.public
: string – Public key - 64 symbols hex stringsecret
: string – Private key - u64 symbols hex string
Signs a data using the provided keys.
type ParamsOfSign = {
unsigned: string,
keys: KeyPair
}
type ResultOfSign = {
signed: string,
signature: string
}
function sign(
params: ParamsOfSign,
): Promise<ResultOfSign>;
function sign_sync(
params: ParamsOfSign,
): ResultOfSign;
NOTE: Sync version is available only for
lib-node
binding.unsigned
: string – Data that must be signed encoded inbase64
.
signed
: string – Signed data combined with signature encoded inbase64
.signature
: string – Signature encoded inhex
.
Verifies signed data using the provided public key. Raises error if verification is failed.
type ParamsOfVerifySignature = {
signed: string,
public: string
}
type ResultOfVerifySignature = {
unsigned: string
}
function verify_signature(
params: ParamsOfVerifySignature,
): Promise<ResultOfVerifySignature>;
function verify_signature_sync(
params: ParamsOfVerifySignature,
): ResultOfVerifySignature;
NOTE: Sync version is available only for
lib-node
binding.signed
: string – Signed data that must be verified encoded inbase64
.public
: string – Signer's public key - 64 symbols hex string
unsigned
: string – Unsigned data encoded inbase64
.
Calculates SHA256 hash of the specified data.
type ParamsOfHash = {
data: string
}
type ResultOfHash = {
hash: string
}
function sha256(
params: ParamsOfHash,
): Promise<ResultOfHash>;
function sha256_sync(
params: ParamsOfHash,
): ResultOfHash;
NOTE: Sync version is available only for
lib-node
binding.data
: string – Input data for hash calculation. Encoded withbase64
.
hash
: string – Hash of inputdata
. Encoded with 'hex'.
Calculates SHA512 hash of the specified data.
type ParamsOfHash = {
data: string
}
type ResultOfHash = {
hash: string
}
function sha512(
params: ParamsOfHash,
): Promise<ResultOfHash>;
function sha512_sync(
params: ParamsOfHash,
): ResultOfHash;
NOTE: Sync version is available only for
lib-node
binding.data
: string – Input data for hash calculation. Encoded withbase64
.
hash
: string – Hash of inputdata
. Encoded with 'hex'.
Perform
scrypt
encryptionDerives key from
password
and key
using scrypt
algorithm. See [https://en.wikipedia.org/wiki/Scrypt].log_n
- The log2 of the Scrypt parameterN
r
- The Scrypt parameterr
p
- The Scrypt parameterp
log_n
must be less than64
r
must be greater than0
and less than or equal to4294967295
p
must be greater than0
and less than4294967295
log_n = 15
(n = 32768
)r = 8
p = 1
type ParamsOfScrypt = {
password: string,
salt: string,
log_n: number,
r: number,
p: number,
dk_len: number
}
type ResultOfScrypt = {
key: string
}
function scrypt(
params: ParamsOfScrypt,
): Promise<ResultOfScrypt>;
function scrypt_sync(
params: ParamsOfScrypt,
): ResultOfScrypt;
NOTE: Sync version is available only for
lib-node
binding.password
: string – The password bytes to be hashed. Must be encoded withbase64
.salt
: string – Salt bytes that modify the hash to protect against Rainbow table attacks. Must be encoded withbase64
.log_n
: number – CPU/memory cost parameterr
: number – The block size parameter, which fine-tunes sequential memory read size and performance.p
: number – Parallelization parameter.dk_len
: number – Intended output length in octets of the derived key.
key
: string – Derived key. Encoded withhex
.
Generates a key pair for signing from the secret key
NOTE: In the result the secret key is actually the concatenation of secret and public keys (128 symbols hex string) by design of NaCL. See also the stackexchange question.
type ParamsOfNaclSignKeyPairFromSecret = {
secret: string
}
type KeyPair = {
public: string,
secret: string
}
function nacl_sign_keypair_from_secret_key(
params: ParamsOfNaclSignKeyPairFromSecret,
): Promise<KeyPair>;
function nacl_sign_keypair_from_secret_key_sync(
params: ParamsOfNaclSignKeyPairFromSecret,
): KeyPair;
NOTE: Sync version is available only for
lib-node
binding.secret
: string – Secret key - unprefixed 0-padded to 64 symbols hex string
public
: string – Public key - 64 symbols hex stringsecret
: string – Private key - u64 symbols hex string
Signs data using the signer's secret key.
type ParamsOfNaclSign = {
unsigned: string,
secret: string
}
type ResultOfNaclSign = {
signed: string
}
function nacl_sign(
params: ParamsOfNaclSign,
): Promise<ResultOfNaclSign>;
function nacl_sign_sync(
params: ParamsOfNaclSign,
): ResultOfNaclSign;
NOTE: Sync version is available only for
lib-node
binding.unsigned
: string – Data that must be signed encoded inbase64
.secret
: string – Signer's secret key - unprefixed 0-padded to 128 symbols hex string (concatenation of 64 symbols secret and 64 symbols public keys). Seenacl_sign_keypair_from_secret_key
.
signed
: string – Signed data, encoded inbase64
.
Verifies the signature and returns the unsigned message
Verifies the signature in
signed
using the signer's public key public
and returns the message unsigned
.If the signature fails verification, crypto_sign_open raises an exception.
type ParamsOfNaclSignOpen = {
signed: string,
public: string
}
type ResultOfNaclSignOpen = {
unsigned: string
}
function nacl_sign_open(
params: ParamsOfNaclSignOpen,
): Promise<ResultOfNaclSignOpen>;
function nacl_sign_open_sync(
params: ParamsOfNaclSignOpen,
): ResultOfNaclSignOpen;
NOTE: Sync version is available only for
lib-node
binding.signed
: string – Signed data that must be unsigned. Encoded withbase64
.public
: string – Signer's public key - unprefixed 0-padded to 64 symbols hex string
unsigned
: string – Unsigned data, encoded inbase64
.
Signs the message using the secret key and returns a signature.
Signs the message
unsigned
using the secret key secret
and returns a signature signature
.type ParamsOfNaclSign = {
unsigned: string,
secret: string
}
type ResultOfNaclSignDetached = {
signature: string
}
function nacl_sign_detached(
params: ParamsOfNaclSign,
): Promise<ResultOfNaclSignDetached>;
function nacl_sign_detached_sync(
params: ParamsOfNaclSign,
): ResultOfNaclSignDetached;
NOTE: Sync version is available only for
lib-node
binding.unsigned
: string – Data that must be signed encoded inbase64
.secret
: string – Signer's secret key - unprefixed 0-padded to 128 symbols hex string (concatenation of 64 symbols secret and 64 symbols public keys). Seenacl_sign_keypair_from_secret_key
.
signature
: string – Signature encoded inhex
.
Verifies the signature with public key and
unsigned
data.type ParamsOfNaclSignDetachedVerify = {
unsigned: string,
signature: string,
public: string
}
type ResultOfNaclSignDetachedVerify = {
succeeded: boolean
}
function nacl_sign_detached_verify(
params: ParamsOfNaclSignDetachedVerify,
): Promise<ResultOfNaclSignDetachedVerify>;
function nacl_sign_detached_verify_sync(
params: ParamsOfNaclSignDetachedVerify,
): ResultOfNaclSignDetachedVerify;
NOTE: Sync version is available only for
lib-node
binding.unsigned
: string – Unsigned data that must be verified. Encoded withbase64
.signature
: string – Signature that must be verified. Encoded withhex
.public
: string – Signer's public key - unprefixed 0-padded to 64 symbols hex string.
succeeded
: boolean –true
if verification succeeded orfalse
if it failed
Generates a random NaCl key pair
type KeyPair = {
public: string,
secret: string
}
function nacl_box_keypair(): Promise<KeyPair>;
function nacl_box_keypair_sync(): KeyPair;
NOTE: Sync version is available only for
lib-node
binding.public
: string – Public key - 64 symbols hex string