Skip to content

Commit

Permalink
Fix hash generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Anatoly Sablin committed Dec 6, 2019
1 parent cbb9fce commit 17ebc2a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/main/java/io/kamax/mxisd/hash/HashEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Base64;
import java.util.List;

public class HashEngine {
Expand All @@ -18,6 +19,7 @@ public class HashEngine {
private final List<? extends IThreePidProvider> providers;
private final HashStorage hashStorage;
private final HashingConfig config;
private final Base64.Encoder base64 = Base64.getUrlEncoder().withoutPadding();
private String pepper;

public HashEngine(List<? extends IThreePidProvider> providers, HashStorage hashStorage, HashingConfig config) {
Expand Down Expand Up @@ -51,7 +53,7 @@ public String getPepper() {
}

protected String hash(ThreePidMapping pidMapping) {
return DigestUtils.sha256Hex(pidMapping.getValue() + " " + pidMapping.getMedium() + " " + getPepper());
return base64.encodeToString(DigestUtils.sha256(pidMapping.getValue() + " " + pidMapping.getMedium() + " " + getPepper()));
}

protected String newPepper() {
Expand Down
7 changes: 5 additions & 2 deletions src/test/java/io/kamax/mxisd/test/hash/HashEngineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import org.apache.commons.codec.digest.DigestUtils;
import org.junit.Test;

import java.util.Base64;

public class HashEngineTest {

@Test
public void sha256test() {
assertEquals("a26de61ae3055f84b33ac1a179b9ad5301f9109024f4db1ae653ea525d2136f4",
DigestUtils.sha256Hex("[email protected] email I9x4vpcWjqp9X8iiOY4a"));
Base64.Encoder encoder = Base64.getUrlEncoder().withoutPadding();
assertEquals("rujYzy1w0JxulN_rVlErGUmkdXT5znL0sjSF_IWreko",
encoder.encodeToString(DigestUtils.sha256("[email protected] email I9x4vpcWjqp9X8iiOY4a")));
}
}

0 comments on commit 17ebc2a

Please sign in to comment.