-
Notifications
You must be signed in to change notification settings - Fork 863
Expand file tree
/
Copy pathCrypto.java
More file actions
41 lines (32 loc) · 1.12 KB
/
Crypto.java
File metadata and controls
41 lines (32 loc) · 1.12 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
package com.github.javafaker;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Crypto {
private final Faker faker;
protected Crypto(Faker faker) {
this.faker = faker;
}
public String md5() {
return generateString("MD5", "%032x");
}
public String sha1() {
return generateString("SHA-1", "%040x");
}
public String sha256() {
return generateString("SHA-256", "%064x");
}
public String sha512() {
return generateString("SHA-512", "%0128x");
}
private String generateString(String algorithm, String format) {
try {
MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
String characters = faker.lorem().characters();
messageDigest.update(characters.getBytes(), 0, characters.length());
return String.format(format, new BigInteger(1, messageDigest.digest()));
} catch (NoSuchAlgorithmException noSuchAlgorithmException) {
throw new RuntimeException(noSuchAlgorithmException);
}
}
}