-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto-utils.js
More file actions
180 lines (157 loc) · 5.04 KB
/
Copy pathcrypto-utils.js
File metadata and controls
180 lines (157 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* TrustBanco Encryption Utilities
* Basic encryption/decryption for localStorage data
* Uses AES-like approach with base64 encoding
*/
const CryptoUtils = (() => {
// Master encryption key (in production, use environment variables)
const MASTER_KEY = 'trustbanco-secure-2026';
const VERSION = '1';
/**
* Simple encryption function
* Converts data to encrypted format with version prefix
*/
function encrypt(data) {
try {
if (!data) return null;
// Convert data to JSON string if it's an object
const dataString = typeof data === 'string' ? data : JSON.stringify(data);
// Create encryption key from master key and timestamp
const encKey = generateKey(dataString.length);
// Simple XOR encryption with base64 encoding
let encrypted = '';
for (let i = 0; i < dataString.length; i++) {
encrypted += String.fromCharCode(
dataString.charCodeAt(i) ^ encKey.charCodeAt(i % encKey.length)
);
}
// Encode to base64
const encoded = btoa(encrypted);
// Add version prefix for future compatibility
return `${VERSION}:${encoded}`;
} catch (error) {
console.error('Encryption error:', error);
return null;
}
}
/**
* Decryption function
* Reverses the encryption process
*/
function decrypt(encryptedData) {
try {
if (!encryptedData) return null;
// Check version prefix
const parts = encryptedData.split(':');
if (parts.length !== 2 || parts[0] !== VERSION) {
console.warn('Invalid encryption version or format');
return null;
}
const encoded = parts[1];
// Decode from base64
let encrypted;
try {
encrypted = atob(encoded);
} catch (e) {
console.error('Base64 decode error:', e);
return null;
}
// Create same encryption key
const encKey = generateKey(encrypted.length);
// Reverse XOR encryption
let decrypted = '';
for (let i = 0; i < encrypted.length; i++) {
decrypted += String.fromCharCode(
encrypted.charCodeAt(i) ^ encKey.charCodeAt(i % encKey.length)
);
}
// Try to parse as JSON, otherwise return as string
try {
return JSON.parse(decrypted);
} catch (e) {
return decrypted;
}
} catch (error) {
console.error('Decryption error:', error);
return null;
}
}
/**
* Generate encryption key based on data and master key
*/
function generateKey(dataLength) {
const key = MASTER_KEY + dataLength.toString();
return key.repeat(Math.ceil(dataLength / key.length)).substring(0, dataLength);
}
/**
* Secure localStorage operations
*/
const StorageManager = {
/**
* Set encrypted data in localStorage
*/
setSecure: function(key, value) {
try {
const encrypted = encrypt(value);
if (encrypted) {
localStorage.setItem(key, encrypted);
return true;
}
return false;
} catch (error) {
console.error('Storage error:', error);
return false;
}
},
/**
* Get decrypted data from localStorage
*/
getSecure: function(key) {
try {
const encrypted = localStorage.getItem(key);
if (!encrypted) return null;
// Try to decrypt
const decrypted = decrypt(encrypted);
if (decrypted) return decrypted;
// Fallback to plain text for backward compatibility
return encrypted;
} catch (error) {
console.error('Retrieval error:', error);
return null;
}
},
/**
* Remove data from localStorage
*/
removeSecure: function(key) {
try {
localStorage.removeItem(key);
return true;
} catch (error) {
console.error('Removal error:', error);
return false;
}
},
/**
* Clear all secure data
*/
clearSecure: function() {
try {
localStorage.clear();
return true;
} catch (error) {
console.error('Clear error:', error);
return false;
}
}
};
return {
encrypt,
decrypt,
StorageManager
};
})();
/**
* Global secure storage shortcuts
*/
const SecureStorage = CryptoUtils.StorageManager;