@@ -23,6 +23,9 @@ import { MessageVerifier } from './message_verifier.ts'
23
23
* security (read more https://en.wikipedia.org/wiki/Semantic_security).
24
24
*/
25
25
export class Encryption {
26
+ /**
27
+ * Configuration options for the encryption instance
28
+ */
26
29
#options: Required < EncryptionOptions >
27
30
28
31
/**
@@ -50,11 +53,18 @@ export class Encryption {
50
53
51
54
/**
52
55
* The algorithm in use
56
+ *
57
+ * @returns The encryption algorithm being used
53
58
*/
54
59
get algorithm ( ) : 'aes-256-cbc' {
55
60
return this . #options. algorithm
56
61
}
57
62
63
+ /**
64
+ * Creates a new Encryption instance with the provided options
65
+ *
66
+ * @param options - Configuration options for encryption
67
+ */
58
68
constructor ( options : EncryptionOptions ) {
59
69
const secretValue =
60
70
options . secret && typeof options . secret === 'object' && 'release' in options . secret
@@ -69,8 +79,10 @@ export class Encryption {
69
79
70
80
/**
71
81
* Validates the app secret
82
+ *
83
+ * @param secret - The secret to validate
72
84
*/
73
- #validateSecret( secret ?: string ) {
85
+ #validateSecret( secret ?: string ) : void {
74
86
if ( typeof secret !== 'string' ) {
75
87
throw new errors . E_MISSING_APP_KEY ( )
76
88
}
@@ -93,8 +105,13 @@ export class Encryption {
93
105
*
94
106
* You can optionally define a purpose for which the value was encrypted and
95
107
* mentioning a different purpose/no purpose during decrypt will fail.
108
+ *
109
+ * @param payload - The data to be encrypted
110
+ * @param expiresIn - Optional expiration time
111
+ * @param purpose - Optional purpose for which the value is encrypted
112
+ * @returns The encrypted payload as a string
96
113
*/
97
- encrypt ( payload : any , expiresIn ?: string | number , purpose ?: string ) {
114
+ encrypt ( payload : any , expiresIn ?: string | number , purpose ?: string ) : string {
98
115
/**
99
116
* Using a random string as the iv for generating unpredictable values
100
117
*/
@@ -132,6 +149,10 @@ export class Encryption {
132
149
133
150
/**
134
151
* Decrypt value and verify it against a purpose
152
+ *
153
+ * @param value - The encrypted value to decrypt
154
+ * @param purpose - Optional purpose that the value was encrypted for
155
+ * @returns The decrypted data if valid, null if decryption fails
135
156
*/
136
157
decrypt < T extends any > ( value : unknown , purpose ?: string ) : T | null {
137
158
if ( typeof value !== 'string' ) {
@@ -191,8 +212,11 @@ export class Encryption {
191
212
192
213
/**
193
214
* Create a children instance with different secret key
215
+ *
216
+ * @param options - Optional configuration options to override
217
+ * @returns A new Encryption instance with the merged options
194
218
*/
195
- child ( options ?: EncryptionOptions ) {
219
+ child ( options ?: EncryptionOptions ) : Encryption {
196
220
return new Encryption ( { ...this . #options, ...options } )
197
221
}
198
222
}
0 commit comments