-
Notifications
You must be signed in to change notification settings - Fork 0
/
password.js
107 lines (98 loc) · 3.25 KB
/
password.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
106
107
/*class Generator
{
public static function generate(WordListInterface $wordList, $lenght = 4, $separator = ' ')
{
$words = array();
foreach (range(1, $lenght) as $number) {
$random = mt_rand() / mt_getrandmax();
$words[] = $wordList->get($random);
}
return join($separator, $words);
}
public static function generateRuTranslit($lenght = 4, $separator = ' ')
{
return self::generate(new WordList\RuTranslit(), $lenght, $separator);
}
public static function generateRu($lenght = 4, $separator = ' ')
{
return self::generate(new WordList\Ru(), $lenght, $separator);
}
public static function generateEn($lenght = 4, $separator = ' ')
{
return self::generate(new WordList\En(), $lenght, $separator);
}
}*/
// curry curry wahahaha
function charcodeat(num) {
return function(str) {
return str.charCodeAt(num);
}
}
function add(a, b) {
return a + b;
}
/*
object yobarandom
has seed
has function generate -> number
*/
var Yoba_Random = function(seed) {
var self = this;
// seed is a string
var seeed = ( // MAGIC
seed // 'abc' as example
.split('') // -> ['a', 'b', 'c']
.map(charcodeat(0)) // -> [97, 98, 99]
.map(Math.sin) // -> [0.555555, 0.12312, -0.2315]
.reduce(add) // -> 123.1123
); // values are random, just to show logic
self.seed = Math.floor(Math.sin(seeed) * 1e10); // yep there is magic const
self.generate = function(max) {
// returns an in in range [0, max)
return Math.floor(Math.abs(Math.sin(self.seed++)) * max)
}
self.choose = function(list) {
// returns random element from list
var n = self.generate(list.length);
return list[n];
}
}
$(document).ready(function() {
var lang = $('select#lang');
var seed = $('input[name="seed"]');
var wordsnumber = $('input[name="wordsnumber"]');
var spaces = $(':checkbox[name="spaces"]')
var button = $('button#gen');
var input = $('input[name="result"]');
var test = $('div#test');
var state = {
lang: lang.val(),
seed: undefined,
randomgenerator: undefined
}
$.getJSON('data.json', function(data) {
button.click(function(){
var langlist = data[lang.val()]; // to choose lang from
var seed_string = seed.val();
if (!seed_string) {
seed.val('я же просил!')
}
// state check
var random;
if (state.lang == lang.val() && state.seed == seed_string &&state.randomgenerator)
random = state.randomgenerator;
else {
random = new Yoba_Random(seed.val());
state.seed = seed_string;
state.randomgenerator = random;
}
var times = parseInt(wordsnumber.val(), 10);
var words = [];
for (var i = 0; i < times; ++i)
words.push(random.choose(langlist));
var separator = spaces.prop('checked')? ' ' : '';
var password = words.join(separator);
input.val(password);
});
});
});