Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure that padding is used in Crypto #745

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/main/java/com/github/javafaker/Crypto.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,27 @@ protected Crypto(Faker faker) {
}

public String md5() {
return generateString("MD5");
return generateString("MD5", "%032x");
}

public String sha1() {
return generateString("SHA-1");
return generateString("SHA-1", "%040x");
}

public String sha256() {
return generateString("SHA-256");
return generateString("SHA-256", "%064x");
}

public String sha512() {
return generateString("SHA-512");
return generateString("SHA-512", "%0128x");
}

private String generateString(String algorithm) {
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 new BigInteger(1, messageDigest.digest()).toString(16);
return String.format(format, new BigInteger(1, messageDigest.digest()));
} catch (NoSuchAlgorithmException noSuchAlgorithmException) {
throw new RuntimeException(noSuchAlgorithmException);
}
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/com/github/javafaker/CryptoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ public class CryptoTest extends AbstractFakerTest {

@Test
public void testMd5() {
assertThat(faker.crypto().md5(), matchesRegularExpression("[a-z\\d]+"));
assertThat(faker.crypto().md5(), matchesRegularExpression("\\b[a-fA-F\\d]{32}\\b"));
}

@Test
public void testSha1() {
assertThat(faker.crypto().sha1(), matchesRegularExpression("[a-z\\d]+"));
assertThat(faker.crypto().sha1(), matchesRegularExpression("\\b[a-fA-F\\d]{40}\\b"));
}

@Test
public void testSha256() {
assertThat(faker.crypto().sha256(), matchesRegularExpression("[a-z\\d]+"));
assertThat(faker.crypto().sha256(), matchesRegularExpression("\\b[a-fA-F\\d]{64}\\b"));
}

@Test
public void testSha512() {
assertThat(faker.crypto().sha512(), matchesRegularExpression("[a-z\\d]+"));
assertThat(faker.crypto().sha512(), matchesRegularExpression("\\b[a-fA-F\\d]{128}\\b"));
}
}