1.0.0 • Published 11 months ago

simple-webcrypto v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

simple-webcrypto

AES, RSA encryption/decryption and SHA, MD5 hashing using Web Crypto API in Node.js Node.js의 Web Crypto API를 이용해 AES, RSA 암·복호화, SHA, MD5 암호화를 지원

Installation

npm i simple-webcrypto

or

yarn add simple-webcrypto

or

pnpm add simple-webcrypto

Usage

import {
  aesEncrypt,
  aesDecrypt,
  generateRSAKeyPair,
  rsaEncrypt,
  rsaDecrypt,
  sha256,
  md5,
} from "crypto-utils";

// Example AES usage
(async () => {
  // AES key should be 128, 192, or 256 bits (16, 24, or 32 bytes)
  const key = crypto.getRandomValues(new Uint8Array(16));
  const message = "Hello, World!";

  const { iv, encrypted } = await aesEncrypt(message, key);
  const decryptedText = await aesDecrypt(encrypted, key, iv);

  console.log("Decrypted Text:", decryptedText);
})();

// Example RSA usage
(async () => {
  const { publicKey, privateKey } = await generateRSAKeyPair();

  const message = "Hello, World!";
  const publicKeyBuffer = new Uint8Array(
    await crypto.subtle.exportKey("spki", publicKey)
  );
  const privateKeyBuffer = new Uint8Array(
    await crypto.subtle.exportKey("pkcs8", privateKey)
  );

  const encrypted = await rsaEncrypt(message, publicKeyBuffer);
  const decryptedText = await rsaDecrypt(encrypted, privateKeyBuffer);

  console.log("Decrypted Text:", decryptedText);
})();

// Example SHA and MD5 usage
(async () => {
  const message = "Hello, World!";

  const sha256Hash = await sha256(message);
  console.log("SHA-256 Hash:", Buffer.from(sha256Hash).toString("hex"));

  const md5Hash = md5(message);
  console.log("MD5 Hash:", md5Hash);
})();

License

Open source licensed as MIT.

1.0.0

11 months ago