Skip to main content
Home

Built and signed on GitHub Actions

🧮 Features based upon Web Crypto APIs

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
a week ago (2.0.5)
function encrypt
encrypt(
message: string,
unnamed 1: { key: CryptoKey | string; length?:
0
| 256
| 512
; }
,
): Promise<string>

Encrypt message with key.

It returns an hexadecimal string structured as follows:

┌──────────────────────┬─────────────────┬────────────┬───────────┬────────────┬╴╴╴╴╴╴╴╴╴╴╴╴╴╴┐
│ Initial Vector [16b] │ Signature [16b] │ Hash [64b] │ Size [8b] │ Value [Xb] │ Padding [Yb] ┊
└──────────────────────┴─────────────────┴────────────┴───────────┴────────────┴╴╴╴╴╴╴╴╴╴╴╴╴╴╴┘

The following are used by the native SubtleCrypto.encrypt API:

  • The initial vector (IV) is used by AES-GCM algorithm to ensure a same input will yield different outputs each time and prevent stream cipher attacks.
  • The signature is used by the AES-GCM algorithm internally

The following are used by this library:

  • The hash (SHA-256) is used to ensure the integrity of the size and value after decryption, while providing extra entropy
  • The size is the length of the secret value in bits, which is used to discard the padding after decryption
  • The value is the actual secret
  • The padding is used to obfuscate the actual value length, while providing extra entropy

The length parameter is used to specify the length of the output hash in bits. Supported values are 256 and 512. If set to 0 instead, padding will be disabled entirely allowing to encrypt larger values but at the cost of leaking the approximate values length. Additionally, if a value size exceed 255 bytes, its integrity will only be checked by the hash field.

import { decrypt, encrypt, exportKey } from "./encryption.ts"
const key = await exportKey({ seed: "", salt: "" })
console.assert(await decrypt(await encrypt("🍱 bento", { key }), { key }) === "🍱 bento")

Parameters

message: string
unnamed 1: { key: CryptoKey | string; length?:
0
| 256
| 512
; }

Return Type

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@libs/crypto

Import symbol

import { encrypt } from "@libs/crypto/encryption";
or

Import directly with a jsr specifier

import { encrypt } from "jsr:@libs/crypto/encryption";

Add Package

pnpm i jsr:@libs/crypto
or (using pnpm 10.8 or older)
pnpm dlx jsr add @libs/crypto

Import symbol

import { encrypt } from "@libs/crypto/encryption";

Add Package

yarn add jsr:@libs/crypto
or (using Yarn 4.8 or older)
yarn dlx jsr add @libs/crypto

Import symbol

import { encrypt } from "@libs/crypto/encryption";

Add Package

vlt install jsr:@libs/crypto

Import symbol

import { encrypt } from "@libs/crypto/encryption";

Add Package

npx jsr add @libs/crypto

Import symbol

import { encrypt } from "@libs/crypto/encryption";

Add Package

bunx jsr add @libs/crypto

Import symbol

import { encrypt } from "@libs/crypto/encryption";