@@ -8,6 +8,11 @@ export const encryptHistory = async (data: any): Promise<ArrayBuffer> => {
8
8
const iv = getIv ( )
9
9
const storedKey = await getKeyFromSessionStorage ( )
10
10
const key = await getOrCreateKey ( storedKey )
11
+
12
+ if ( ! key ) {
13
+ throw new Error ( 'Unable to encrypt history' )
14
+ }
15
+
11
16
const encrypted = await encryptData ( iv , key , data )
12
17
13
18
return encrypted
@@ -34,6 +39,12 @@ const encryptData = async (iv: Uint8Array, key: CryptoKey, data: any) => {
34
39
throw new Error ( 'Unable to encrypt history' )
35
40
}
36
41
42
+ if ( typeof window . crypto . subtle === 'undefined' ) {
43
+ console . warn ( 'Encryption is not supported in this environment. SSL is required.' )
44
+
45
+ return Promise . resolve ( data )
46
+ }
47
+
37
48
const textEncoder = new TextEncoder ( )
38
49
const str = JSON . stringify ( data )
39
50
const encoded = new Uint8Array ( str . length )
@@ -51,6 +62,12 @@ const encryptData = async (iv: Uint8Array, key: CryptoKey, data: any) => {
51
62
}
52
63
53
64
const decryptData = async ( iv : Uint8Array , key : CryptoKey , data : any ) => {
65
+ if ( typeof window . crypto . subtle === 'undefined' ) {
66
+ console . warn ( 'Decryption is not supported in this environment. SSL is required.' )
67
+
68
+ return Promise . resolve ( data )
69
+ }
70
+
54
71
const decrypted = await window . crypto . subtle . decrypt (
55
72
{
56
73
name : 'AES-GCM' ,
@@ -78,6 +95,12 @@ const getIv = () => {
78
95
}
79
96
80
97
const createKey = async ( ) => {
98
+ if ( typeof window . crypto . subtle === 'undefined' ) {
99
+ console . warn ( 'Encryption is not supported in this environment. SSL is required.' )
100
+
101
+ return Promise . resolve ( null )
102
+ }
103
+
81
104
return window . crypto . subtle . generateKey (
82
105
{
83
106
name : 'AES-GCM' ,
@@ -89,6 +112,12 @@ const createKey = async () => {
89
112
}
90
113
91
114
const saveKey = async ( key : CryptoKey ) => {
115
+ if ( typeof window . crypto . subtle === 'undefined' ) {
116
+ console . warn ( 'Encryption is not supported in this environment. SSL is required.' )
117
+
118
+ return Promise . resolve ( )
119
+ }
120
+
92
121
const keyData = await window . crypto . subtle . exportKey ( 'raw' , key )
93
122
94
123
SessionStorage . set ( historySessionStorageKeys . key , Array . from ( new Uint8Array ( keyData ) ) )
@@ -101,6 +130,10 @@ const getOrCreateKey = async (key: CryptoKey | null) => {
101
130
102
131
const newKey = await createKey ( )
103
132
133
+ if ( ! newKey ) {
134
+ return null
135
+ }
136
+
104
137
await saveKey ( newKey )
105
138
106
139
return newKey
0 commit comments