-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword.ts
More file actions
45 lines (36 loc) · 1.23 KB
/
password.ts
File metadata and controls
45 lines (36 loc) · 1.23 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
export class passwordGenerator {
private password: string;
private id: number = 6;
constructor(password: string, id?: number) {
this.password = password;
if(id) {
this.id = id;
}
}
public async getPassword(): Promise<string> {
// calculate password: $ID$SALT$HASH
const salt = this.generateSalt();
// generate the hash
const p = await Deno.run({
cmd: ["openssl", "passwd", "-6", "--salt", salt, "-stdin", "<<<", '"'+this.password+'"' ],
stdin: "piped",
stdout: "piped",
});
await p.status();
const hash = await p.output();
// return the password
return `\$${this.id}\$${salt}\$${hash}`;
}
private generateSalt(): string {
return this.generateRandomString(16);
}
private generateRandomString(length:number): string {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
}