forked from heusalagroup/fi.hg.backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CryptoServiceImpl.ts
61 lines (51 loc) · 1.58 KB
/
CryptoServiceImpl.ts
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
// Copyright (c) 2021-2023. Heusala Group Oy <info@heusalagroup.fi>. All rights reserved.
import { randomInt } from "crypto";
import { CryptoService } from "../core/crypto/CryptoService";
export class CryptoServiceImpl implements CryptoService {
protected constructor () {
}
public static create () : CryptoService {
return CryptoServiceImpl;
}
/**
* Creates random string containing numbers between 0 and 9.
*
* Eg. `size=2` gives values between 0 and 99.
* Eg. `size=4` gives values between 0 and 9999.
*
* @param size
*/
public static createRandomInteger (
size: number
) : number {
if (size <= 0) {
throw new TypeError(`CryptoService.createRandomNumberString: size must be over 0: ${size} provided`);
}
return randomInt(0, Math.pow(10, size)-1 );
}
/**
* Creates random string containing numbers between 0 and 9.
*
* Eg. `size=2` gives values between "00" and "99".
* Eg. `size=4` gives values between "0000" and "9999".
*
* @param size
*/
public static createRandomIntegerString (
size: number
) : string {
return `${CryptoServiceImpl.createRandomInteger(size)}`.padStart(size, "0");
}
/**
* @inheritDoc
*/
public createRandomInteger (size: number): number {
return CryptoServiceImpl.createRandomInteger(size);
}
/**
* @inheritDoc
*/
public createRandomIntegerString (size: number): string {
return CryptoServiceImpl.createRandomIntegerString(size);
}
}