-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve readme and add quickstart tests
- Loading branch information
1 parent
ba7e1a0
commit 38fe53f
Showing
5 changed files
with
119 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
modules/id-mask/src/test/java/at/favre/lib/idmask/QuickstartTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package at.favre.lib.idmask; | ||
|
||
import at.favre.lib.bytes.Bytes; | ||
import org.junit.Test; | ||
|
||
import java.security.SecureRandom; | ||
import java.security.Security; | ||
import java.util.Random; | ||
import java.util.UUID; | ||
|
||
import static org.junit.Assert.assertArrayEquals; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
public class QuickstartTests { | ||
|
||
@Test | ||
public void quickstartLong() { | ||
byte[] key = Bytes.random(16).array(); | ||
long id = new Random().nextLong(); | ||
|
||
IdMask<Long> idMask = IdMaskFactory.createForLongIds( | ||
Config.builder().keyManager(KeyManager.Factory.with(key)).build()); | ||
|
||
String maskedId = idMask.mask(id); | ||
//example: wMDHT8QN6Ljyko3ma5QLEIE | ||
long originalId = idMask.unmask(maskedId); | ||
|
||
assertEquals(id, originalId); | ||
System.out.println(maskedId); | ||
} | ||
|
||
@Test | ||
public void quickstartUUID() { | ||
byte[] key = Bytes.random(16).array(); | ||
UUID id = UUID.fromString("eb1c6999-5fc1-4d5f-b98a-792949c38c45"); | ||
|
||
IdMask<UUID> idMask = IdMaskFactory.createForUuids( | ||
Config.builder().keyManager(KeyManager.Factory.with(key)).build()); | ||
|
||
String maskedId = idMask.mask(id); | ||
//example: rK0wpnG1lwvG0xiZn5swxOYmAvxhA4A7yg | ||
UUID originalId = idMask.unmask(maskedId); | ||
|
||
assertEquals(id, originalId); | ||
System.out.println(maskedId); | ||
} | ||
|
||
@Test | ||
public void fullExample() { | ||
byte[] key = Bytes.random(16).array(); | ||
byte[] id128bit = Bytes.random(16).array(); | ||
|
||
IdMask<byte[]> idMask = IdMaskFactory.createFor128bitNumbers( | ||
Config.builder() | ||
.keyManager(KeyManager.Factory.with(key)) | ||
.randomizedIds(true) //non-deterministic output | ||
.enableCache(true) | ||
.cacheImpl(new Cache.SimpleLruMemCache(64)) | ||
.encoding(new ByteToTextEncoding.Base32()) | ||
.secureRandom(new SecureRandom()) | ||
.securityProvider(Security.getProvider("BC")) | ||
.build()); | ||
|
||
String maskedId = idMask.mask(id128bit); | ||
//example: RAKESQ32V37ORV5JX7FAWCFVV2PITRN5KMOKYBJBLNS42WCEA3FI2FIBXLKJGMTSZY | ||
byte[] originalId = idMask.unmask(maskedId); | ||
|
||
assertArrayEquals(id128bit, originalId); | ||
System.out.println(maskedId); | ||
System.out.println(Bytes.wrap(originalId).encodeHex()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters