@@ -72,9 +72,9 @@ The following errors may be thrown:
7272
7373## Example
7474
75- Let's say you have a session ID in a server and you want the server to encrypt and store the ID in a cookie .
75+ This is a simple demonstration; production uses should utilize key rotation, among many other security measures .
7676
77- ` ./lib/session/ crypto.ts `
77+ ` ./lib/crypto/message .ts `
7878
7979``` typescript
8080import {
@@ -83,25 +83,25 @@ import {
8383 decryptSymmetricallyText
8484} from " singlecrypt-text" ;
8585
86- import { getSessionEncryptionKey } from " ./lib/crypto/session " ;
86+ import { getMessageEncryptionKey } from " ./lib/crypto/key " ;
8787
8888
89- const sessionCryptoKey = await createSymmetricKeyWithText (
90- await getSessionEncryptionKey ()
89+ const messageCryptoKey = await createSymmetricKeyWithText (
90+ await getMessageEncryptionKey ()
9191);
9292
9393
94- export async function encryptSessionId (value : string ) {
94+ export async function encryptMessageId (value : string ) {
9595 return await encryptSymmetricallyText (
9696 value ,
97- sessionCryptoKey
97+ messageCryptoKey
9898 );
9999}
100100
101- export async function decryptSessionId (value : string ) {
101+ export async function decryptMessageId (value : string ) {
102102 return await decryptSymmetricallyText (
103103 value ,
104- sessionCryptoKey
104+ messageCryptoKey
105105 );
106106}
107107```
@@ -115,30 +115,29 @@ import {
115115 decryptSymmetricallyText
116116} from " singlecrypt-text" ;
117117
118- import { getSessionEncryptionKey } from " ./lib/crypto/session " ;
118+ import { getMessageEncryptionKey } from " ./lib/crypto/key " ;
119119
120120
121121const textEncoder = new TextEncoder ();
122122const textDecoder = new TextDecoder ();
123123
124- const sessionCryptoKey = await createSymmetricKeyWithText (
125- await getSessionEncryptionKey (),
126- textEncoder
124+ const messageCryptoKey = await createSymmetricKeyWithText (
125+ await getMessageEncryptionKey ()
127126);
128127
129128
130- export async function encryptSessionId (value : string ) {
129+ export async function encryptMessageId (value : string ) {
131130 return await encryptSymmetricallyText (
132131 value ,
133- sessionCryptoKey ,
132+ messageCryptoKey ,
134133 textEncoder
135134 );
136135}
137136
138- export async function decryptSessionId (value : string ) {
137+ export async function decryptMessageId (value : string ) {
139138 return await decryptSymmetricallyText (
140139 value ,
141- sessionCryptoKey ,
140+ messageCryptoKey ,
142141 textDecoder
143142 );
144143}
@@ -148,15 +147,15 @@ And now you can easily encrypt and decrypt session IDs:
148147
149148``` typescript
150149// ...
151- import { encryptSessionId , decryptSessionId } from " ./lib/session/ crypto" ;
150+ import { encryptMessageId , decryptMessageId } from " ./lib/crypto/message.ts " ;
152151// ...
153152
154- const sessionId = await getSessionIdFromDatabase ();
155- const encryptedId = await encryptSessionId ( sessionId ); // Now you can safely store it in an HttpOnly cookie
153+ const message = await getMessage ();
154+ const encryptedMessage = await encryptMessageId ( message ); // Now you can safely store it in an HttpOnly cookie
156155// ...
157- const decryptedId = await decryptSessionId ( encryptedId );
156+ const decryptedMessage = await decryptMessageId ( encryptedMessage );
158157// ...
159- console .log (sessionId === decryptedId ); // True
158+ console .log (message === decryptedMessage ); // True
160159// ...
161160```
162161
0 commit comments