This repository has been archived by the owner on May 11, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dict.js
105 lines (87 loc) · 3.42 KB
/
dict.js
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
/**
* Created by Paweł Krawczyk on 02/04/15.
* https://ipsec.pl/
*/
"use strict";
/** @const */ var /** string */ DEFAULT_LANG = 'english';
/** @const */ var /** string */ LS_TARGET_ENTROPY = 'target_entropy';
/**
* Validate if input language is a supported one
* @param {string} lang
*/
function validDictionary(/** string */ lang) {
// manually managed list of supported dictionaries
switch (lang) {
case 'english':
break;
case 'russian':
break;
case 'polish':
break;
default:
lang = DEFAULT_LANG;
}
return lang;
}
// choose a dictionary for specified language and display the first passphrase
/**
* Load new dictionary for specified language.
* @param {string} lang
*/
function loadDictionary(/** string */ lang) {
// validate requested language
lang = validDictionary(lang);
console.log('initialising dictionary, lang=', lang);
var dict_path = 'dict/' + lang + '.js';
// unhide the "loading dictionary" message
document.getElementById('loading-dictionary').hidden = false;
// hide passphrase output field
document.getElementById('output').hidden = true;
// delete any previously injected tags
// just in case, there should be none
var e = document.getElementById('dictionary');
if (e) {
e.remove();
}
// prepare script tag to load new dictionary
var script = document.createElement('script');
var head = document.head || document.getElementsByTagName('head')[0] || document.documentElement;
script.src = dict_path;
script.id = 'dictionary';
var done = false;
// hook function that will be called when dictionary is loaded
script.onload = script.onreadystatechange = function () {
if (!done && (!document.readyState || document.readyState == "loaded" || document.readyState == "complete" || document.readyState == "interactive" )) {
done = true;
// hide "loading dictionary" message
document.getElementById('loading-dictionary').hidden = true;
// unhide passphrase field
document.getElementById('output').hidden = false;
// init the PassGen with new dictionary
window.pp = new PassGen(dictionary, 'output');
window.ee = new EntropyEstimator(dictionary);
outputNewPassphrase();
// clean up in DOM
script.onload = script.onreadystatechange = null;
head.removeChild(script);
// update dictionary params display
document.getElementById('dictionary-size').textContent = dictionary.dictionary_size.toFixed();
document.getElementById('alphabet-size').textContent = dictionary.alphabet_size.toFixed();
document.getElementById('entropy-per-character').textContent = dictionary.max_entropy_per_char.toFixed(2);
document.getElementById('entropy-per-word').textContent = dictionary.entropy_per_word.toFixed(2);
}
};
// update the selection list to display the current language
var o = document.getElementById("lang").options;
for (var i = 0; i < o.length; i++) {
if (o[i].value == lang) {
o[i].selected = true;
}
}
// save current language selection
if ('localStorage' in window) {
localStorage.setItem(LS_LANGUAGE, lang);
}
// actually inject the tag
head.insertBefore(script, head.firstChild);
}