-
Notifications
You must be signed in to change notification settings - Fork 17
/
ecrypt.php
47 lines (36 loc) · 1.26 KB
/
ecrypt.php
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
<?php
class ECrypt
{
/* alphabet to be used when generating OTPs */
// (tagit bort l,1,I,0,O,o från ursprunglig)
private $alpha = "abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789";
/**
Generates an array of one-time passwords (OTPs).
6 chars should be sufficient with 62**6 (56e9) possible values.
Should only be generated once every annual election!
@param (number) The number of OTPs to generate
@return A list of OTPs
*/
public function generate_otp($number) {
$otp_array = array();
$count = 0;
while($count < $number) {
$pass = $this->generateRandomString(5, $this->alpha);
// don't want collisions
if (in_array($pass, $otp_array)) continue;
// add element to array, efficient way
$otp_array[] = $pass;
$count++;
}
return $otp_array;
}
function generateRandomString($nbrOfLetters, $availableLetters){
$charactersLength = strlen($availableLetters);
$randomString = '';
for ($i = 0; $i < $nbrOfLetters; $i++) {
$randomString .= $availableLetters[random_int(0, $charactersLength - 1)];
}
return $randomString;
}
}
?>