crypttool/src/lib/pwgen.ts

23 lines
675 B
TypeScript

import { writable} from 'svelte/store';
import { passwordLength } from './generalStore';
let length: number = 16;
passwordLength.subscribe(value => {
length = value;
})
function generateRandomString(length: number): string {
let result = '';
while (result.length < length) {
const array = new Uint8Array(1);
self.crypto.getRandomValues(array);
const charCode: number = array[0];
if ( 33 < charCode && charCode < 126 ) {
result += String.fromCharCode(charCode + (charCode < 26 ? 65 : (charCode < 52 ? 71 : -4)));
}
}
return result;
}
const password: string = generateRandomString(length);
export let globalPassword = writable(password);