npm package, cjs -> esm...

This commit is contained in:
2024-10-07 21:30:18 +03:00
parent 28ce8c6fb3
commit 9ea2dd5f69
15 changed files with 410 additions and 292 deletions

View File

@@ -4,29 +4,48 @@ Cryptographic functions provider and example repository.
This module provides encryption and decryption functions for handling both binary data and strings using public and private keys. It utilizes `ecies-wasm` for the underlying cryptographic operations.
Package available on npm: https://www.npmjs.com/package/@zklx/crypto
## Functions
### `encryptFile(publicKey: Key, fileName: string, data: Uint8Array): Uint8Array`
Encrypts the given binary data for the recipient using the public key. The file name is included in the encrypted data.
- `publicKey`: The recipient's public key.
- `fileName`: The name of the file being encrypted.
- `data`: The plaintext data as a `Uint8Array`.
Returns the ciphertext as a `Uint8Array`.
### `decryptFile(privateKey: Key, data: Uint8Array): { data: Uint8Array, fileName: string }`
Decrypts the given ciphertext using the recipient's private key. Returns the decrypted data and the original file name.
- `privateKey`: The recipient's private key.
- `data`: The ciphertext data as a `Uint8Array`.
Returns an object containing the plaintext data and the original file name.
### `encryptString(publicKey: Key, string: string): string`
Encrypts the given string with the recipient's public key. Returns the ciphertext as a hexadecimal string.
Encrypts the given string for the recipient using their public key. Returns the ciphertext as a hexadecimal string.
- `publicKey`: The recipient's public key.
- `string`: The plaintext string.
### `decryptString(privateKey: Key, string: string): string`
Decrypts the given ciphertext string (in hexadecimal format) with the recipient's private key. Returns the plaintext string.
Decrypts the given ciphertext string (in hexadecimal format) using the recipient's private key. Returns the plaintext string.
### `encryptFile(publicKey: Key, data: Uint8Array): Uint8Array`
Encrypts the given binary data (`Uint8Array`) with the recipient's public key. Returns the ciphertext as a `Uint8Array`.
### `decryptFile(privateKey: Key, data: Uint8Array): Uint8Array`
Decrypts the given ciphertext data (`Uint8Array`) with the recipient's private key. Returns the plaintext data as a `Uint8Array`.
- `privateKey`: The recipient's private key.
- `string`: The ciphertext string in hexadecimal format.
## Usage
```javascript
import { Key } from "./zkl-kds/key";
import { encryptString, decryptString, encryptFile, decryptFile } from "./crypto";
import { Key } from "@zklx/kds";
import { encryptString, decryptString, encryptFile, decryptFile } from "@zklx/crypto";
// Example usage:
@@ -43,12 +62,14 @@ const decryptedString = decryptString(privateKey, encryptedString);
console.log(decryptedString); // "Hello, World!"
// Encrypt binary data
const fileName = "document.txt";
const data = new Uint8Array([/* ... */]);
const encryptedData = encryptFile(publicKey, data);
const encryptedData = encryptFile(publicKey, fileName, data);
// Decrypt the binary data
const decryptedData = decryptFile(privateKey, encryptedData);
const { data: decryptedData, fileName: decryptedFileName } = decryptFile(privateKey, encryptedData);
console.log(decryptedFileName); // "document.txt"
console.log(decryptedData); // Uint8Array
```
@@ -61,8 +82,6 @@ pnpm install
npx webpack # use npx, pnpm - webpack integration is broken
```
then navigate to index.html.
## Error Handling
This module performs type checks on its inputs. It throws `TypeError` if arguments are not of the expected types. Additionally, the `decryptString` function issues a warning if the provided ciphertext string does not appear to be a valid hexadecimal string.
@@ -76,4 +95,9 @@ This module performs type checks on its inputs. It throws `TypeError` if argumen
This project is licensed under the GNU Lesser General Public License, version 2.1.
Yigid BALABAN, <fyb@fybx.dev>
Authored by Yigid BALABAN, fyb@fybx.dev
2024 © zk-Lokomotive team
https://zk-lokomotive.xyz/