### Run Quick Start Example Source: https://github.com/ecies/js/blob/master/README.md Instructions to install dependencies, build the project, and run the quick start example from the example directory. ```bash $ pnpm install && pnpm build && cd example/runtime && pnpm install && node quick-start.js ``` -------------------------------- ### Install eciesjs Source: https://github.com/ecies/js/blob/master/README.md Install the eciesjs package using npm. ```bash npm install eciesjs ``` -------------------------------- ### Quick Start Encryption and Decryption Source: https://github.com/ecies/js/blob/master/README.md Encrypts a message using a public key and decrypts it using the corresponding private key. Ensure TextEncoder and TextDecoder are available in your runtime. ```typescript import { PrivateKey, decrypt, encrypt } from "eciesjs"; const encoder = new TextEncoder(); const decoder = new TextDecoder(); const sk = new PrivateKey(); const data = encoder.encode("hello world🌍"); const decrypted = decrypt(sk.secret, encrypt(sk.publicKey.toBytes(), data)); console.log(decoder.decode(decrypted)); ``` -------------------------------- ### Get secp256k1 Public Keys (Compressed/Uncompressed) Source: https://github.com/ecies/js/blob/master/DETAILS.md Shows how to obtain secp256k1 public keys in both uncompressed (65 bytes) and compressed (33 bytes) formats from a private key. ```typescript import { secp256k1 } from '@noble/curves/secp256k1'; const privateKey = 3n; const publicKeyUncompressed = secp256k1.getPublicKey(privateKey, false); // 65 bytes const publicKeyCompressed = secp256k1.getPublicKey(privateKey, true); // 33 bytes ``` -------------------------------- ### AES-256-GCM Encryption and Decryption Source: https://github.com/ecies/js/blob/master/DETAILS.md Basic example of encrypting and decrypting data using AES-256-GCM with a provided key and nonce. The key should be derived securely, and a unique nonce must be used for each encryption with the same key. ```typescript import { gcm } from '@noble/ciphers/aes'; // 32-byte key from ECDH const key = new Uint8Array(32); // 16-byte nonce const nonce = new Uint8Array(16); const data = new TextEncoder().encode('hello world'); // Encrypt const cipher = gcm(key, nonce); const encrypted = cipher.encrypt(data); // Decrypt const decipher = gcm(key, nonce); const decrypted = decipher.decrypt(encrypted); console.log(new TextDecoder().decode(decrypted)); // 'hello world' ``` -------------------------------- ### Initialize Theme in ECIES.JS Source: https://github.com/ecies/js/blob/master/example/browser/index.html This snippet initializes the theme for ECIES.JS by checking local storage or user preferences. It sets the 'theme' dataset on the document element. ```javascript (() => { const stored = localStorage.getItem("eciesjs-theme"); const prefersLight = window.matchMedia("(prefers-color-scheme: light)").matches; document.documentElement.dataset.theme = stored || (prefersLight ? "light" : "dark"); })(); ``` -------------------------------- ### PublicKey Class Methods Source: https://github.com/ecies/js/blob/master/README.md Static method to create a PublicKey from a hex string and instance methods for key conversion, decapsulation, and comparison. ```typescript static fromHex(hex: string, curve?: EllipticCurve): PublicKey; constructor(data: Uint8Array, curve?: EllipticCurve); toSizes(compressed?: boolean): Uint8Array; toHex(compressed?: boolean): string; decapsulate(sk: PrivateKey, compressed?: boolean): Uint8Array; equals(other: PublicKey): boolean; ``` -------------------------------- ### PrivateKey Class Methods Source: https://github.com/ecies/js/blob/master/README.md Static method to create a PrivateKey from a hex string and instance methods for key manipulation and comparison. ```typescript static fromHex(hex: string, curve?: EllipticCurve): PrivateKey; constructor(secret?: Uint8Array, curve?: EllipticCurve); toHex(): string; encapsulate(pk: PublicKey, compressed?: boolean): Uint8Array; multiply(pk: PublicKey, compressed?: boolean): Uint8Array; equals(other: PrivateKey): boolean; ``` -------------------------------- ### Configuration Source: https://github.com/ecies/js/blob/master/README.md Defines configuration options for ECIES operations, including elliptic curve, key formats, and symmetric cipher algorithms. ```APIDOC ## Configuration ### Description Allows customization of ECIES operations. Ensure consistent configuration between encryption and decryption. ### Configuration Options - **ellipticCurve**: `"secp256k1" | "x25519" | "ed25519"` - The elliptic curve to use. - **isEphemeralKeyCompressed**: `boolean` - Whether the ephemeral key in the payload is compressed (only for secp256k1). - **isHkdfKeyCompressed**: `boolean` - Whether the shared elliptic curve key in key derivation is compressed (only for secp256k1). - **symmetricAlgorithm**: `"aes-256-gcm" | "xchacha20"` - The symmetric cipher algorithm to use. - **symmetricNonceLength**: `12 | 16` - The nonce length in bytes (only for AES-256-GCM). ### Default Configuration `ECIES_CONFIG` provides the default configuration: ```ts export const ECIES_CONFIG = new Config(); ``` ### Example Usage ```typescript import { Config } from "eciesjs"; const customConfig = new Config(); customConfig.ellipticCurve = "x25519"; customConfig.symmetricAlgorithm = "xchacha20"; // Use customConfig when calling encrypt or decrypt // const encrypted = encrypt(publicKey, data, customConfig); ``` ``` -------------------------------- ### ECIES Configuration Options Source: https://github.com/ecies/js/blob/master/README.md Defines the configuration options for ECIES, including elliptic curve type, key formats, and symmetric cipher algorithm. Ensure consistent configuration across applications for compatibility. ```typescript export type EllipticCurve = "secp256k1" | "x25519" | "ed25519"; export type SymmetricAlgorithm = "aes-256-gcm" | "xchacha20"; export type NonceLength = 12 | 16; export class Config { ellipticCurve: EllipticCurve = "secp256k1"; isEphemeralKeyCompressed: boolean = false; isHkdfKeyCompressed: boolean = false; symmetricAlgorithm: SymmetricAlgorithm = "aes-256-gcm"; symmetricNonceLength: NonceLength = 16; } export const ECIES_CONFIG = new Config(); ``` -------------------------------- ### Generate ECDH Shared Secret with secp256k1 Source: https://github.com/ecies/js/blob/master/DETAILS.md Demonstrates generating public keys from private keys and calculating the shared secret using ECDH. Ensure private keys are securely generated in production. ```typescript import { secp256k1 } from '@noble/curves/secp256k1'; import { equalBytes } from "@noble/ciphers/utils"; // Generate private keys (in production, use crypto.getRandomValues()) const k1 = 3n; const k2 = 2n; // Get public keys const pub1 = secp256k1.getPublicKey(k1); const pub2 = secp256k1.getPublicKey(k2); // Calculate shared secret - both parties will get the same result const shared1 = secp256k1.getSharedSecret(k1, pub2); const shared2 = secp256k1.getSharedSecret(k2, pub1); console.log(equalBytes(shared1, shared2)); // true ``` -------------------------------- ### PublicKey Class Source: https://github.com/ecies/js/blob/master/README.md Represents a public key for cryptographic operations. Provides methods for key creation, conversion, and verification. ```APIDOC ## PublicKey ### Description Represents a public key. Allows for key creation, conversion to/from hex, and cryptographic operations. ### Methods #### `static fromHex(hex: string, curve?: EllipticCurve): PublicKey` Creates a PublicKey instance from a hex string. #### `constructor(data: Uint8Array, curve?: EllipticCurve)` Constructs a PublicKey instance from raw bytes. #### `toBytes(compressed?: boolean): Uint8Array` Converts the public key to raw bytes, optionally compressed. #### `toHex(compressed?: boolean): string` Converts the public key to a hex string, optionally compressed. #### `decapsulate(sk: PrivateKey, compressed?: boolean): Uint8Array` Decapsulates a shared secret using the corresponding private key. #### `equals(other: PublicKey): boolean` Compares this public key with another. ### Properties None explicitly documented beyond methods. ``` -------------------------------- ### PrivateKey Class Source: https://github.com/ecies/js/blob/master/README.md Represents a private key for cryptographic operations. Provides methods for key generation, manipulation, and conversion. ```APIDOC ## PrivateKey ### Description Represents a private key. Allows for key generation, conversion to/from hex, and cryptographic operations. ### Methods #### `static fromHex(hex: string, curve?: EllipticCurve): PrivateKey` Creates a PrivateKey instance from a hex string. #### `constructor(secret?: Uint8Array, curve?: EllipticCurve)` Constructs a PrivateKey instance. If `secret` is not provided, a new key is generated. #### `toHex(): string` Converts the private key to a hex string. #### `encapsulate(pk: PublicKey, compressed?: boolean): Uint8Array` Encapsulates a public key with the private key. #### `multiply(pk: PublicKey, compressed?: boolean): Uint8Array` Performs scalar multiplication with a public key. #### `equals(other: PrivateKey): boolean` Compares this private key with another. ### Properties #### `secret: Uint8Array` Gets the raw secret bytes of the private key. #### `publicKey: PublicKey` Gets the corresponding public key. ``` -------------------------------- ### encrypt Source: https://github.com/ecies/js/blob/master/README.md Encrypts data using the receiver's public key. Supports various configurations for elliptic curve, key formats, and symmetric ciphers. ```APIDOC ## encrypt ### Description Encrypts data using the receiver's public key. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **receiverRawPK** (string | Uint8Array) - Required - Receiver's public key, hex `string` or `Uint8Array`. - **data** (Uint8Array) - Required - Data to encrypt. - **config** (Config) - Optional - Configuration object for encryption. ### Request Example ```typescript import { encrypt } from "eciesjs"; const receiverPublicKey = "..."; // hex string or Uint8Array const dataToEncrypt = new Uint8Array([1, 2, 3]); const encryptedData = encrypt(receiverPublicKey, dataToEncrypt); ``` ### Response #### Success Response (200) - **encryptedData** (Uint8Array) - The encrypted data. #### Response Example ```typescript // Example response structure (actual bytes will vary) const encryptedData = new Uint8Array([...]); ``` ``` -------------------------------- ### decrypt Source: https://github.com/ecies/js/blob/master/README.md Decrypts data using the receiver's private key. Requires the same configuration as encryption. ```APIDOC ## decrypt ### Description Decrypts data using the receiver's private key. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **receiverRawSK** (string | Uint8Array) - Required - Receiver's private key, hex `string` or `Uint8Array`. - **data** (Uint8Array) - Required - Data to decrypt. - **config** (Config) - Optional - Configuration object for decryption. ### Request Example ```typescript import { decrypt } from "eciesjs"; const receiverPrivateKey = "..."; // hex string or Uint8Array const encryptedData = new Uint8Array([...]); // Data received from encrypt function const decryptedData = decrypt(receiverPrivateKey, encryptedData); ``` ### Response #### Success Response (200) - **decryptedData** (Uint8Array) - The decrypted data. #### Response Example ```typescript // Example response structure (actual bytes will vary) const decryptedData = new Uint8Array([1, 2, 3]); ``` ``` -------------------------------- ### Derive AES Key using HKDF-SHA256 Source: https://github.com/ecies/js/blob/master/DETAILS.md Securely derives a 32-byte AES key from an ECDH shared secret using HKDF with SHA256. This is preferred over plain SHA256 for key derivation. ```typescript import { hkdf } from '@noble/hashes/hkdf'; import { sha256 } from '@noble/hashes/sha2'; // Derive AES key from ECDH shared secret const ourPrivateKey = 3n; // const ourPublicKey = secp256k1.getPublicKey(ourPrivateKey); const theirPrivateKey = 2n; const theirPublicKey = secp256k1.getPublicKey(theirPrivateKey); const sharedSecret = secp256k1.getSharedSecret(ourPrivateKey, theirPublicKey); const sharedKey = hkdf(sha256, sharedSecret, undefined, undefined, 32); ``` -------------------------------- ### PrivateKey Class Properties Source: https://github.com/ecies/js/blob/master/README.md Access the secret key as Uint8Array and the associated public key object. ```typescript get secret(): Uint8Array; readonly publicKey: PublicKey; ``` -------------------------------- ### Adjust Encrypted Data for ECIES Payload Format Source: https://github.com/ecies/js/blob/master/DETAILS.md This code snippet demonstrates how to reorder the components (nonce, tag, ciphertext) of AES-GCM output to match the ECIES payload structure (nonce || tag || cipherText). ```javascript const encrypted = cipher.encrypt(data); const cipherTextLength = encrypted.length - tagLength; const cipherText = encrypted.subarray(0, cipherTextLength); const tag = encrypted.subarray(cipherTextLength); // ecies payload format: pk || nonce || tag || cipherText const adjustedEncrypted = concatBytes(nonce, tag, cipherText); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.