@@ -27,78 +27,70 @@ pnpm add singlecrypt-text
2727bun add singlecrypt-text
2828```
2929
30- ## Methods
30+ ## Examples
3131
32- ### ` createSymmetricKeyWithText `
32+ This is a simple demonstration; production uses should utilize ** key rotation ** , among many other security measures.
3333
34- Creates a symmetric ` CryptoKey ` object to be used in the following methods. This method also converts your value key to a SHA-256 hash. It takes two parameters:
34+ There are two ways to use this library: [ object-oriented ] ( #object-oriented ) (recommended) or [ functional-oriented ] ( #functional-oriented ) .
3535
36- 1 . A string key to be hashed. A 32-byte random string is recommended.
37- 2 . A ` TextEncoder ` instance, if you want to reuse it (optional).
36+ ### Object-oriented
3837
39- Returns a ` Promise<CryptoKey> ` containing a SHA-256 hash used to encrypt and decrypt strings.
40-
41- A ` TypeError ` may be thrown if there are problems with the string key.
42-
43- ### ` encryptTextSymmetrically `
44-
45- Encrypts a value with a symmetric ` CryptoKey ` previously generated. It takes four parameters:
46-
47- 1 . A string value to be encrypted.
48- 2 . The symmetric key generated with ` createSymmetricKeyWithText ` .
49- 3 . A ` TextEncoder ` instance, if you want to reuse it (optional).
50- 4 . A boolean ` urlSafe ` that indicates if the ` base64url ` alphabet should be used instead of ` base64 ` . It defaults to ` false ` .
51-
52- Returns a ` Promise<string> ` containing the encrypted value.
38+ ` ./lib/crypto/message.ts `
5339
54- A ` DOMException ` may be thrown if the key is invalid or if the operation failed (e.g., AES-GCM plaintext longer than 2^39−256 bytes).
40+ ``` typescript
41+ import SingleCryptText from " singlecrypt-text" ;
5542
56- ### ` decryptTextSymmetrically `
43+ import { getMessageEncryptionKey } from " ./lib/crypto/key " ;
5744
58- Decrypts a value with a symmetric ` CryptoKey ` previously generated. It takes three parameters:
5945
60- 1 . A string value to be decrypted.
61- 2 . The symmetric key generated with ` createSymmetricKeyWithText ` .
62- 3 . A ` TextDecoder ` instance, if you want to reuse it (optional).
63- 4 . A boolean ` urlSafe ` that indicates if the ` base64url ` alphabet should be used instead of ` base64 ` . It defaults to ` false ` .
46+ export const cryptoMessage = new SingleCryptText (await getMessageEncryptionKey ())
47+ ```
6448
65- Returns a ` Promise<string> ` containing the decrypted value.
49+ #### Usage
6650
67- The following errors may be thrown :
51+ And now you can easily encrypt and decrypt messages :
6852
69- - ` TypeError ` : if the first parameter is not a string.
70- - ` SyntaxError ` : if the first parameter contains characters outside the Base64 alphabet.
71- - ` DOMException ` : if the key is invalid or if the operation failed.
53+ ``` typescript
54+ // ...
55+ import { cryptoMessage } from " ./lib/crypto/message.ts" ;
56+ // ...
7257
73- ## Example
58+ const message = await getMessage ();
59+ const encryptedMessage = await cryptoMessage .encrypt (message ); // Now you can safely store it in an HttpOnly cookie
60+ // ...
61+ const decryptedMessage = await cryptoMessage .decrypt (encryptedMessage );
62+ // ...
63+ console .log (message === decryptedMessage ); // True
64+ // ...
65+ ```
7466
75- This is a simple demonstration; production uses should utilize key rotation, among many other security measures.
67+ ### Functional-oriented
7668
7769` ./lib/crypto/message.ts `
7870
7971``` typescript
8072import {
81- createSymmetricKeyWithText ,
73+ createSymmetricKeyFromText ,
8274 encryptTextSymmetrically ,
8375 decryptTextSymmetrically
8476} from " singlecrypt-text" ;
8577
8678import { getMessageEncryptionKey } from " ./lib/crypto/key" ;
8779
8880
89- const messageCryptoKey = await createSymmetricKeyWithText (
81+ const messageCryptoKey = await createSymmetricKeyFromText (
9082 await getMessageEncryptionKey ()
9183);
9284
9385
94- export async function encryptMessageId (value : string ) {
86+ export async function encryptMessage (value : string ) {
9587 return await encryptTextSymmetrically (
9688 value ,
9789 messageCryptoKey
9890 );
9991}
10092
101- export async function decryptMessageId (value : string ) {
93+ export async function decryptMessage (value : string ) {
10294 return await decryptTextSymmetrically (
10395 value ,
10496 messageCryptoKey
@@ -110,7 +102,7 @@ Or you can reuse `TextEncoder` and `TextDecoder` instances for slightly better p
110102
111103``` typescript
112104import {
113- createSymmetricKeyWithText ,
105+ createSymmetricKeyFromText ,
114106 encryptTextSymmetrically ,
115107 decryptTextSymmetrically
116108} from " singlecrypt-text" ;
@@ -121,20 +113,20 @@ import { getMessageEncryptionKey } from "./lib/crypto/key";
121113const textEncoder = new TextEncoder ();
122114const textDecoder = new TextDecoder ();
123115
124- const messageCryptoKey = await createSymmetricKeyWithText (
116+ const messageCryptoKey = await createSymmetricKeyFromText (
125117 await getMessageEncryptionKey ()
126118);
127119
128120
129- export async function encryptMessageId (value : string ) {
121+ export async function encryptMessage (value : string ) {
130122 return await encryptTextSymmetrically (
131123 value ,
132124 messageCryptoKey ,
133125 textEncoder
134126 );
135127}
136128
137- export async function decryptMessageId (value : string ) {
129+ export async function decryptMessage (value : string ) {
138130 return await decryptTextSymmetrically (
139131 value ,
140132 messageCryptoKey ,
@@ -143,22 +135,144 @@ export async function decryptMessageId(value: string) {
143135}
144136```
145137
138+ #### Usage
139+
146140And now you can easily encrypt and decrypt messages:
147141
148142``` typescript
149143// ...
150- import { encryptMessageId , decryptMessageId } from " ./lib/crypto/message.ts" ;
144+ import { encryptMessage , decryptMessage } from " ./lib/crypto/message.ts" ;
151145// ...
152146
153147const message = await getMessage ();
154- const encryptedMessage = await encryptMessageId (message ); // Now you can safely store it in an HttpOnly cookie
148+ const encryptedMessage = await encryptMessage (message ); // Now you can safely store it in an HttpOnly cookie
155149// ...
156- const decryptedMessage = await decryptMessageId (encryptedMessage );
150+ const decryptedMessage = await decryptMessage (encryptedMessage );
157151// ...
158152console .log (message === decryptedMessage ); // True
159153// ...
160154```
161155
156+ ## Reference
157+
158+ ### Object-oriented API: ` SingleCryptText `
159+
160+ A class that simplifies symmetric encryption and decryption using a shared key derived from a text string.
161+
162+ It is also the default export.
163+
164+ ``` ts
165+ new SingleCryptText (
166+ text : string ,
167+ urlSafe : boolean = true ,
168+ extractable : boolean = false ,
169+ textEncoder ?: TextEncoder ,
170+ textDecoder ?: TextDecoder
171+ )
172+ ```
173+ - ` text ` : The secret string to use as a key (should be high-entropy, such as a 32-byte random string).
174+ - ` urlSafe ` (optional): Use ` base64url ` encoding (` true ` , default) or regular ` base64 ` (` false ` ).
175+ - ` extractable ` (optional): Whether the generated cryptographic key is extractable. Defaults to ` false ` .
176+ - ` textEncoder ` /` textDecoder ` (optional): Optionally reuse your own encoder/decoder instances.
177+
178+ #### Instance methods
179+
180+ - ` async encrypt(text: string): Promise<string> `
181+
182+ Encrypt a string using the instance's key.
183+
184+ - ` async decrypt(encryptedText: string): Promise<string> `
185+
186+ Decrypt a string previously encrypted by this or any compatible instance.
187+
188+ - ` async getKey(): Promise<CryptoKey> `
189+
190+ Returns the underlying ` CryptoKey ` instance.
191+
192+ #### Instance properties
193+
194+ - ` urlSafe: boolean `
195+ Indicates if the instance uses ` base64url ` encoding (` true ` , default) or standard ` base64 ` (` false ` ) for encrypted outputs.
196+
197+
198+ #### Example
199+
200+ ``` ts
201+ import SingleCryptText from " singlecrypt-text" ;
202+
203+ const crypt = new SingleCryptText (" my secret passphrase" );
204+
205+ const encrypted = await crypt .encrypt (" Sensitive message!" );
206+ const decrypted = await crypt .decrypt (encrypted );
207+
208+ console .log (decrypted ); // "Sensitive message!"
209+ ```
210+
211+ ---
212+
213+
214+ ### Functional-oriented API
215+
216+ Functional exports for direct cryptographic operations.
217+
218+ ``` ts
219+ createSymmetricKeyFromText (
220+ text : string ,
221+ extractable ?: boolean ,
222+ textEncoder ?: TextEncoder
223+ ): Promise < CryptoKey >
224+ ```
225+ - ` text ` : The secret string to use as a key (should be high-entropy, such as a 32-byte random string).
226+ - ` extractable ` (optional): Whether the generated key is extractable. Defaults to ` false ` .
227+ - ` textEncoder ` (optional): Optionally reuse your own ` TextEncoder ` instance.
228+
229+ Returns a ` Promise<CryptoKey> ` containing the derived symmetric key (SHA-256 hash).
230+
231+ ** Throws:**
232+ - ` TypeError ` if there are problems with the text key.
233+
234+ ---
235+
236+ ``` ts
237+ encryptTextSymmetrically (
238+ key : CryptoKey ,
239+ text : string ,
240+ urlSafe ?: boolean ,
241+ textEncoder ?: TextEncoder
242+ ): Promise < string >
243+ ```
244+ - ` key ` : A symmetric key previously generated with ` createSymmetricKeyFromText ` .
245+ - ` text ` : String value to encrypt.
246+ - ` urlSafe ` (optional): Use ` base64url ` encoding if ` true ` (default: ` true ` ). If ` false ` , uses regular ` base64 ` .
247+ - ` textEncoder ` (optional): Optionally reuse your own ` TextEncoder ` instance.
248+
249+ Returns a ` Promise<string> ` containing the encrypted value as a Base64 string.
250+
251+ ** Throws:**
252+ - ` DOMException ` if the key is invalid or if the operation failed (e.g., large payload).
253+
254+ ---
255+
256+ ``` ts
257+ decryptTextSymmetrically (
258+ key : CryptoKey ,
259+ encryptedText : string ,
260+ urlSafe ?: boolean ,
261+ textDecoder ?: TextDecoder
262+ ): Promise < string >
263+ ```
264+ - ` key ` : A symmetric key previously generated with ` createSymmetricKeyFromText ` .
265+ - ` encryptedText ` : The string value to decrypt (encrypted with compatible methods/settings).
266+ - ` urlSafe ` (optional): If ` true ` (default), expects ` base64url ` ; if ` false ` , expects regular ` base64 ` .
267+ - ` textDecoder ` (optional): Optionally reuse your own ` TextDecoder ` instance.
268+
269+ Returns a ` Promise<string> ` containing the decrypted value.
270+
271+ ** Throws:**
272+ - ` TypeError ` : if the second parameter is not a string.
273+ - ` SyntaxError ` : if the input contains characters outside the Base64 alphabet.
274+ - ` DOMException ` : if the key is invalid or if the operation failed.
275+
162276---
163277
164278Created by [ SSbit01] ( https://ssbit01.github.io/ ) .
0 commit comments