@10dimen/crypto-toolkit
v1.0.4
Published
**CryptoToolkit** is a comprehensive encryption and data security library designed for web applications. It provides easy-to-use functions for encrypting and decrypting data, as well as managing encryption keys. With support for **AES-GCM** and **RSA-OAEP
Downloads
8
Readme
CryptoToolkit
CryptoToolkit is a powerful and flexible encryption library designed for modern web applications. It provides comprehensive utilities for data encryption, decryption, and key management, supporting AES-GCM and RSA-OAEP algorithms. Additionally, it includes functionalities for hashing data using SHA-512.
Features
- Key Management: Easily generate, export, and import public and private keys.
- Encryption and Decryption: Securely encrypt and decrypt data with advanced algorithms.
- Data Storage: Store encrypted data in
localStorage
orsessionStorage
. - Hashing: Compute SHA-512 hashes of data for integrity checks and more.
- User-Friendly API: Integrate encryption features seamlessly into your applications.
Installation
To install CryptoToolkit
, you can use npm or yarn:
npm install @10dimen/crypto-toolkit
Or with yarn:
yarn add @10dimen/crypto-toolkit
Usage
Encryption and Decryption
Encrypt and store data:
import { CryptoUtils } from '@10dimen/crypto-toolkit';
// Encrypt and store data in localStorage
await CryptoUtils.encryptAndStore('myKey', 'mySecretData');
Decrypt data from storage:
import { CryptoUtils } from '@10dimen/crypto-toolkit';
// Retrieve and decrypt data from localStorage
const decryptedData = await CryptoUtils.decryptFromStorage('myKey');
console.log(decryptedData); // Output: 'mySecretData'
Key Management
Generate a new key pair:
const keys = await CryptoUtils.generateKey();
console.log(keys);
Export and import keys:
// Export public key
const publicKey = await CryptoUtils.exportPublicKey();
console.log(publicKey);
// Import public key
const importedPublicKey = await CryptoUtils.importPublicKey(publicKey);
Hashing
Hash data using SHA-512:
const hashedData = await CryptoUtils.hashSHA512('myData');
console.log(hashedData);
Note: MD5 hashing is not supported by the Web Crypto API and is not included in this library. For MD5 hashing, consider using additional libraries.
API Reference
CryptoUtils.encrypt(data: string | ArrayBuffer): Promise<ArrayBuffer>
Encrypts the provided data using RSA-OAEP.
- data: The data to encrypt (can be a string or an
ArrayBuffer
).
CryptoUtils.decrypt(encryptedData: ArrayBuffer): Promise<string>
Decrypts the provided data using RSA-OAEP.
- encryptedData: The encrypted data to decrypt (must be an
ArrayBuffer
).
CryptoUtils.encryptAndStore(key: string, data: string, storage: string = 'localStorage'): Promise<void>
Encrypts the data and stores it in the specified storage.
- key: The key under which the data will be stored.
- data: The data to encrypt and store.
- storage: Storage type, either
'localStorage'
or'sessionStorage'
.
CryptoUtils.decryptFromStorage(key: string, storage: string = 'localStorage'): Promise<string | null>
Retrieves and decrypts data from the specified storage.
- key: The key under which the data is stored.
- storage: Storage type, either
'localStorage'
or'sessionStorage'
.
CryptoUtils.hashSHA512(data: string): Promise<string>
Hashes the provided data using SHA-512.
- data: The data to hash.
Contributing
We welcome contributions! To contribute:
- Fork the repository.
- Create a new branch (
git checkout -b feature/YourFeature
). - Make your changes.
- Commit your changes (
git commit -am 'Add new feature'
). - Push to the branch (
git push origin feature/YourFeature
). - Open a Pull Request.
Please refer to the CONTRIBUTING.md for more details.
License
This project is licensed under the MIT License - see the LICENSE file for details.