-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.html
More file actions
348 lines (301 loc) · 14.3 KB
/
Copy pathtest.html
File metadata and controls
348 lines (301 loc) · 14.3 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SmartPassLib Test Suite</title>
<style>
body {
font-family: monospace;
max-width: 900px;
margin: 0 auto;
padding: 20px;
background: #1e1e1e;
color: #d4d4d4;
}
h1, h2 { color: #2a82da; }
.test-section {
background: #2d2d2d;
border: 1px solid #3c3c3c;
border-radius: 5px;
padding: 15px;
margin: 15px 0;
}
button {
background: #2a82da;
color: white;
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
margin: 5px;
}
button:hover { background: #1a6ab0; }
input {
width: 100%;
padding: 8px;
margin: 5px 0;
background: #3c3c3c;
border: 1px solid #555;
color: white;
border-radius: 3px;
font-family: monospace;
}
.result {
background: #1e1e1e;
padding: 10px;
border-radius: 5px;
margin-top: 10px;
word-break: break-all;
}
.mode-selector {
display: flex;
gap: 10px;
margin: 10px 0;
flex-wrap: wrap;
}
.mode-btn {
background: #444;
padding: 8px 16px;
}
.mode-btn.active {
background: #2a82da;
}
.password-display {
font-size: 18px;
font-weight: bold;
color: #4ec9b0;
}
</style>
</head>
<body>
<h1>🔐 SmartPassLib v4.0.0 Test Suite</h1>
<p>Cross-platform deterministic password generator for JavaScript</p>
<hr>
<div class="test-section">
<h2>📝 Interactive Test</h2>
<label>Secret phrase (min 12 chars):</label>
<input type="text" id="testSecret" value="MyCatHippo2026">
<label>Password length:</label>
<input type="text" id="testLength" value="16" maxlength="3">
<div class="mode-selector">
<button id="modeSmart" class="mode-btn active">🔑 Smart</button>
<button id="modeStrong" class="mode-btn">💪 Strong</button>
<button id="modeBase" class="mode-btn">📝 Base</button>
<button id="modeCode" class="mode-btn">🔢 Code</button>
</div>
<button id="testGenerateBtn">Generate Password</button>
<div id="testResult" class="result"></div>
</div>
<div class="test-section">
<h2>🧪 Run Test Suite</h2>
<button id="runTestsBtn">Run All Tests</button>
<div id="testOutput" class="result"></div>
</div>
<div class="test-section">
<h2>📖 API Reference</h2>
<pre>
SmartPassLib.VERSION
SmartPassLib.CHARS
SmartPassLib.generatePrivateKey(secret)
SmartPassLib.generatePublicKey(secret)
SmartPassLib.verifySecret(secret, pk)
SmartPassLib.generateSmartPassword(secret, length)
SmartPassLib.generateStrongPassword(length)
SmartPassLib.generateBasePassword(length)
SmartPassLib.generateCode(length)
</pre>
</div>
<script src="smartpasslib.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
let currentMode = 'smart';
const modeSmart = document.getElementById('modeSmart');
const modeStrong = document.getElementById('modeStrong');
const modeBase = document.getElementById('modeBase');
const modeCode = document.getElementById('modeCode');
const testSecret = document.getElementById('testSecret');
const testLength = document.getElementById('testLength');
const testGenerateBtn = document.getElementById('testGenerateBtn');
const runTestsBtn = document.getElementById('runTestsBtn');
const testResult = document.getElementById('testResult');
const testOutput = document.getElementById('testOutput');
function validateAndNormalizeLength(lengthValue, mode) {
if (lengthValue === null || lengthValue === undefined || lengthValue === '') {
return 12;
}
let length = parseInt(lengthValue);
if (isNaN(length) || length < 1) {
return 12;
}
const min = (mode === 'code') ? 4 : 12;
const max = 100;
if (length < min) return min;
if (length > max) return max;
return length;
}
function updateLengthField(mode) {
if (!testLength) return;
let currentVal = testLength.value.trim();
let normalized = validateAndNormalizeLength(currentVal, mode);
const min = (mode === 'code') ? 4 : 12;
testLength.min = min;
testLength.max = 100;
if (currentVal === '' || isNaN(parseInt(currentVal)) || parseInt(currentVal) < min) {
testLength.value = normalized;
} else if (parseInt(currentVal) > 100) {
testLength.value = 100;
} else {
testLength.value = parseInt(currentVal);
}
}
if (testLength) {
testLength.addEventListener('input', function(e) {
let cleaned = this.value.replace(/[^\d]/g, '');
if (cleaned === '') {
this.value = '';
return;
}
let num = parseInt(cleaned);
if (!isNaN(num)) {
const min = (currentMode === 'code') ? 4 : 12;
if (num < min) {
this.value = min;
} else if (num > 100) {
this.value = 100;
} else {
this.value = num;
}
} else {
this.value = cleaned;
}
});
testLength.addEventListener('blur', function() {
if (this.value.trim() === '') {
this.value = (currentMode === 'code') ? 8 : 16;
}
});
}
function setActiveMode(mode) {
[modeSmart, modeStrong, modeBase, modeCode].forEach(btn => {
if (btn) btn.classList.remove('active');
});
if (mode === 'smart' && modeSmart) modeSmart.classList.add('active');
if (mode === 'strong' && modeStrong) modeStrong.classList.add('active');
if (mode === 'base' && modeBase) modeBase.classList.add('active');
if (mode === 'code' && modeCode) modeCode.classList.add('active');
currentMode = mode;
updateLengthField(mode);
}
if (modeSmart) modeSmart.onclick = function() { setActiveMode('smart'); };
if (modeStrong) modeStrong.onclick = function() { setActiveMode('strong'); };
if (modeBase) modeBase.onclick = function() { setActiveMode('base'); };
if (modeCode) modeCode.onclick = function() { setActiveMode('code'); };
if (testGenerateBtn) {
testGenerateBtn.onclick = async function() {
const secret = testSecret ? testSecret.value : '';
let lengthRaw = testLength ? testLength.value : '16';
let length = validateAndNormalizeLength(lengthRaw, currentMode);
if (testLength) testLength.value = length;
if (currentMode !== 'code' && (!secret || secret.length < 12)) {
testResult.innerHTML = '<span style="color: #f48771">❌ Secret phrase must be at least 12 characters</span>';
return;
}
try {
let password, modeName, extraInfo = '';
switch(currentMode) {
case 'smart':
modeName = 'Smart Password';
const publicKey = await SmartPassLib.generatePublicKey(secret);
const privateKey = await SmartPassLib.generatePrivateKey(secret);
const isValid = await SmartPassLib.verifySecret(secret, publicKey);
password = await SmartPassLib.generateSmartPassword(secret, length);
extraInfo = `<strong>Public Key:</strong><br>${publicKey}<br><br>
<strong>Verification:</strong> ${isValid ? '✅ Valid' : '❌ Invalid'}<br><br>`;
break;
case 'strong':
modeName = 'Strong Random';
password = await SmartPassLib.generateStrongPassword(length);
break;
case 'base':
modeName = 'Base Random';
password = await SmartPassLib.generateBasePassword(length);
break;
case 'code':
modeName = 'Auth Code';
password = await SmartPassLib.generateCode(length);
break;
default:
password = await SmartPassLib.generateSmartPassword(secret, length);
modeName = 'Smart Password';
}
testResult.innerHTML = `
<strong>✅ ${modeName}</strong><br><br>
${extraInfo}
<strong>Generated (${length} chars):</strong><br>
<span class="password-display">${password}</span>
`;
} catch (err) {
testResult.innerHTML = `<span style="color: #f48771">❌ Error: ${err.message}</span>`;
console.error(err);
}
};
}
if (runTestsBtn) {
runTestsBtn.onclick = async function() {
testOutput.innerHTML = '<span style="color: #2a82da">Running tests...</span>';
const results = [];
try {
const pwd = await SmartPassLib.generateSmartPassword("MyCatHippo2026", 16);
results.push({ name: "Smart password generation", passed: pwd && pwd.length === 16 });
} catch (e) { results.push({ name: "Smart password", passed: false, error: e.message }); }
try {
const secret = "Test123!@#Secret";
const p1 = await SmartPassLib.generateSmartPassword(secret, 20);
const p2 = await SmartPassLib.generateSmartPassword(secret, 20);
results.push({ name: "Determinism", passed: p1 === p2 });
} catch (e) { results.push({ name: "Determinism", passed: false, error: e.message }); }
try {
const p1 = await SmartPassLib.generateSmartPassword("SecretOne123456", 16);
const p2 = await SmartPassLib.generateSmartPassword("SecretTwo123456", 16);
results.push({ name: "Different secrets", passed: p1 !== p2 });
} catch (e) { results.push({ name: "Different secrets", passed: false, error: e.message }); }
try {
const pwd = await SmartPassLib.generateStrongPassword(16);
results.push({ name: "Strong random", passed: pwd && pwd.length === 16 });
} catch (e) { results.push({ name: "Strong random", passed: false, error: e.message }); }
try {
const pwd = await SmartPassLib.generateBasePassword(16);
results.push({ name: "Base random", passed: pwd && pwd.length === 16 });
} catch (e) { results.push({ name: "Base random", passed: false, error: e.message }); }
try {
const code = await SmartPassLib.generateCode(8);
results.push({ name: "Code generation", passed: code && code.length === 8 });
} catch (e) { results.push({ name: "Code generation", passed: false, error: e.message }); }
try {
const pubKey = await SmartPassLib.generatePublicKey("Test123!@#Secret");
const privKey = await SmartPassLib.generatePrivateKey("Test123!@#Secret");
results.push({ name: "Key generation", passed: pubKey && privKey && pubKey !== privKey });
} catch (e) { results.push({ name: "Key generation", passed: false, error: e.message }); }
try {
let passed = false;
try {
await SmartPassLib.generateSmartPassword("short", 16);
} catch (e) {
passed = e.message.includes('at least 12');
}
results.push({ name: "Min length (12 chars)", passed: passed });
} catch (e) { results.push({ name: "Min length", passed: false, error: e.message }); }
const passedCount = results.filter(r => r.passed).length;
let html = `<strong>📊 Results: ${passedCount}/${results.length} passed</strong><br><br>`;
results.forEach(r => {
html += `${r.passed ? '✅' : '❌'} ${r.name}${r.error ? ` - ${r.error}` : ''}<br>`;
});
testOutput.innerHTML = html;
};
}
updateLengthField('smart');
});
</script>
</body>
</html>