A collection of useful crypto utilities for Polkadot ecosystem projects
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Secure JSON encryption and decryption with password protection, supporting multiple encoding formats and versions for secure data storage and transmission.
Encrypt data to password-protected JSON format.
/**
* Encrypt data to JSON format with password protection
* @param data - Data bytes to encrypt
* @param contentType - Array describing content type
* @param passphrase - Password for encryption
* @returns Encrypted JSON object
*/
function jsonEncrypt(data: Uint8Array, contentType: string[], passphrase: string): EncryptedJson;
/**
* Format encrypted data as JSON
* @param encoded - Encoded encrypted data
* @param contentType - Optional content type array
* @returns Formatted encrypted JSON
*/
function jsonEncryptFormat(encoded: string, contentType?: string[]): EncryptedJson;Decrypt password-protected JSON data.
/**
* Decrypt encrypted JSON with passphrase
* @param json - Encrypted JSON object
* @param passphrase - Password for decryption
* @returns Decrypted data bytes
*/
function jsonDecrypt(json: EncryptedJson, passphrase: string): Uint8Array;
/**
* Decrypt encrypted data directly
* @param encrypted - Encrypted data object
* @param passphrase - Password for decryption
* @returns Decrypted data bytes
*/
function jsonDecryptData(encrypted: EncryptedJson, passphrase: string): Uint8Array;type EncryptedJsonVersion = '0' | '1' | '2' | '3';
type EncryptedJsonEncoding = 'none' | 'scrypt' | 'xsalsa20-poly1305';
interface EncryptedJsonDescriptor {
/** Descriptor for the content */
content: string[];
/** The encoding (in current/latest versions this is always an array) */
type: EncryptedJsonEncoding | EncryptedJsonEncoding[];
/** The version of encoding applied */
version: EncryptedJsonVersion;
}
interface EncryptedJson {
/** The encoded string */
encoded: string;
/** The encoding used */
encoding: EncryptedJsonDescriptor;
}import { jsonEncrypt, jsonDecrypt } from "@polkadot/util-crypto";
// Data to encrypt
const secretData = new TextEncoder().encode("sensitive information");
const contentType = ["pkcs8", "sr25519"];
const passphrase = "strong-password";
// Encrypt data
const encrypted = jsonEncrypt(secretData, contentType, passphrase);
console.log(encrypted.encoding.version); // Current version
console.log(encrypted.encoding.type); // Encoding methods used
// Decrypt data
const decrypted = jsonDecrypt(encrypted, passphrase);
const decryptedText = new TextDecoder().decode(decrypted);
console.log(decryptedText); // "sensitive information"Install with Tessl CLI
npx tessl i tessl/npm-polkadot--util-crypto