Modules@robinpath/encrypt
encrypt

@robinpath/encrypt

0.1.1Node.jsPublic

AES and RSA encryption/decryption with key generation

Encrypt

AES-256-GCM and RSA encryption/decryption with key generation, password-based key derivation, and hashing

Package: @robinpath/encrypt | Category: Utility | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the encrypt module when you need to:

  • Encrypt text with AES using a password (auto-generates salt/IV) -- Use encrypt.aesEncrypt to perform this operation
  • Decrypt AES-encrypted data using a password -- Use encrypt.aesDecrypt to perform this operation
  • Encrypt text with a raw hex key (for advanced use) -- Use encrypt.aesEncryptRaw to perform this operation
  • Generate a cryptographically secure random key -- Use encrypt.generateKey to perform this operation
  • Generate an RSA key pair -- Use encrypt.rsaGenerateKeys to perform this operation

Quick Reference

FunctionDescriptionReturns
aesEncryptEncrypt text with AES using a password (auto-generates salt/IV){encrypted, iv, salt, algorithm, tag}
aesDecryptDecrypt AES-encrypted data using a passwordDecrypted plaintext
aesEncryptRawEncrypt text with a raw hex key (for advanced use){encrypted, iv, algorithm, tag}
generateKeyGenerate a cryptographically secure random keyHex-encoded random key
rsaGenerateKeysGenerate an RSA key pair{publicKey, privateKey} in PEM format
rsaEncryptEncrypt text with an RSA public keyBase64-encoded ciphertext
rsaDecryptDecrypt RSA-encrypted text with a private keyDecrypted plaintext
hashHash a string (sha256, sha512, md5, etc.)Hex digest
deriveKeyDerive an encryption key from a password using scrypt{key, salt}
randomIvGenerate a random initialization vectorHex-encoded IV

Functions

aesEncrypt

Encrypt text with AES using a password (auto-generates salt/IV)

Module: encrypt | Returns: object -- {encrypted, iv, salt, algorithm, tag}

encrypt.aesEncrypt "secret data" "my-password"
ParameterTypeRequiredDescription
plaintextstringYesText to encrypt
passwordstringYesEncryption password
algorithmstringNoAES algorithm (default aes-256-gcm)

aesDecrypt

Decrypt AES-encrypted data using a password

Module: encrypt | Returns: string -- Decrypted plaintext

encrypt.aesDecrypt $encryptedData "my-password"
ParameterTypeRequiredDescription
dataobjectYesEncrypted data object from aesEncrypt
passwordstringYesDecryption password

aesEncryptRaw

Encrypt text with a raw hex key (for advanced use)

Module: encrypt | Returns: object -- {encrypted, iv, algorithm, tag}

encrypt.aesEncryptRaw "data" $hexKey
ParameterTypeRequiredDescription
plaintextstringYesText to encrypt
keystringYesHex-encoded key (32 bytes for AES-256)
algorithmstringNoAES algorithm

generateKey

Generate a cryptographically secure random key

Module: encrypt | Returns: string -- Hex-encoded random key

encrypt.generateKey 256
ParameterTypeRequiredDescription
bitsnumberNoKey size in bits (128, 192, 256)

rsaGenerateKeys

Generate an RSA key pair

Module: encrypt | Returns: object -- {publicKey, privateKey} in PEM format

encrypt.rsaGenerateKeys 4096
ParameterTypeRequiredDescription
bitsnumberNoKey size (2048, 4096)

rsaEncrypt

Encrypt text with an RSA public key

Module: encrypt | Returns: string -- Base64-encoded ciphertext

encrypt.rsaEncrypt "secret" $publicKey
ParameterTypeRequiredDescription
plaintextstringYesText to encrypt
publicKeystringYesPEM-encoded public key

rsaDecrypt

Decrypt RSA-encrypted text with a private key

Module: encrypt | Returns: string -- Decrypted plaintext

encrypt.rsaDecrypt $encrypted $privateKey
ParameterTypeRequiredDescription
ciphertextstringYesBase64-encoded ciphertext
privateKeystringYesPEM-encoded private key

hash

Hash a string (sha256, sha512, md5, etc.)

Module: encrypt | Returns: string -- Hex digest

encrypt.hash "my data" "sha512"
ParameterTypeRequiredDescription
datastringYesData to hash
algorithmstringNoHash algorithm (default sha256)

deriveKey

Derive an encryption key from a password using scrypt

Module: encrypt | Returns: object -- {key, salt}

encrypt.deriveKey "my-password"
ParameterTypeRequiredDescription
passwordstringYesPassword
saltstringNoSalt (auto-generated if omitted)
keyLengthnumberNoKey length in bytes (default 32)

randomIv

Generate a random initialization vector

Module: encrypt | Returns: string -- Hex-encoded IV

encrypt.randomIv 12
ParameterTypeRequiredDescription
bytesnumberNoIV size in bytes (default 16)

Error Handling

All functions throw on failure. Common errors:

ErrorCause
(standard errors)Check function parameters and authentication
@desc "Aes encrypt and validate result"
do
  set $result as encrypt.aesEncrypt "secret data" "my-password"
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. Multi-step Encrypt workflow

Chain multiple encrypt operations together.

@desc "Aes encrypt, aes decrypt, and more"
do
  set $r_aesEncrypt as encrypt.aesEncrypt "secret data" "my-password"
  set $r_aesDecrypt as encrypt.aesDecrypt $encryptedData "my-password"
  set $r_aesEncryptRaw as encrypt.aesEncryptRaw "data" $hexKey
  print "All operations complete"
enddo

2. Safe aesEncrypt with validation

Check results before proceeding.

@desc "Aes encrypt and validate result"
do
  set $result as encrypt.aesEncrypt "secret data" "my-password"
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.1latest1 months ago
Install
$ robinpath add @robinpath/encrypt

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.1
LicenseMIT
Unpacked Size4.8 KB
Versions1
Weekly Downloads26
Total Downloads26
Stars0
Last Publish1 months ago
Created1 months ago

Category

utilities