diff options
author | Bjørn Christian Seime <bjorncs@yahooinc.com> | 2023-01-30 11:45:49 +0100 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@yahooinc.com> | 2023-01-30 11:45:49 +0100 |
commit | f74fcc5254f39bf2244a1beeada0a9b143507d4d (patch) | |
tree | 889c6ddd8648706ab4c033f2f0b5ed9fab09e668 /security-utils | |
parent | e01512af63118a1ae19d49a1350a3ed544a7a4c0 (diff) |
Add y64 encoder
Diffstat (limited to 'security-utils')
-rw-r--r-- | security-utils/src/main/java/com/yahoo/security/YBase64.java | 38 | ||||
-rw-r--r-- | security-utils/src/test/java/com/yahoo/security/YBase64Test.java | 27 |
2 files changed, 65 insertions, 0 deletions
diff --git a/security-utils/src/main/java/com/yahoo/security/YBase64.java b/security-utils/src/main/java/com/yahoo/security/YBase64.java new file mode 100644 index 00000000000..3c0c61c6d89 --- /dev/null +++ b/security-utils/src/main/java/com/yahoo/security/YBase64.java @@ -0,0 +1,38 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +package com.yahoo.security; + +import java.util.Base64; + +/** + * Variant of {@link java.util.Base64} with the following modifications: + * - {@code +} is replaced by {@code .} + * - {@code /} is replaced by {code _} + * - {@code =} is replaced by {code -} + * + * @author bjorncs + */ +public class YBase64 { + private YBase64() {} + + public static byte[] decode(byte[] in) { + byte[] rewritten = new byte[in.length]; + for (int i = 0; i < in.length; i++) { + if (in[i] == '.') rewritten[i] = '+'; + else if (in[i] == '_') rewritten[i] = '/'; + else if (in[i] == '-') rewritten[i] = '='; + else rewritten[i] = in[i]; + } + return Base64.getDecoder().decode(rewritten); + } + + public static byte[] encode(byte[] in) { + byte[] encoded = Base64.getEncoder().encode(in); + for (int i = 0; i < encoded.length; i++) { + if (encoded[i] == '+') encoded[i] = '.'; + else if (encoded[i] == '/') encoded[i] = '_'; + else if (encoded[i] == '=') encoded[i] = '-'; + } + return encoded; + } +} diff --git a/security-utils/src/test/java/com/yahoo/security/YBase64Test.java b/security-utils/src/test/java/com/yahoo/security/YBase64Test.java new file mode 100644 index 00000000000..5853b7a6ef2 --- /dev/null +++ b/security-utils/src/test/java/com/yahoo/security/YBase64Test.java @@ -0,0 +1,27 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +package com.yahoo.security; + +import org.junit.jupiter.api.Test; + +import java.util.Base64; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * @author bjorncs + */ +class YBase64Test { + + @Test + void encodesAndDecodesCorrectly() { + var base64Encoded = "+abcd/01234="; + byte[] raw = Base64.getDecoder().decode(base64Encoded); + assertEquals("+abcd/01234=", new String(Base64.getEncoder().encode(raw))); + byte[] ybase64Decoded = YBase64.encode(raw); + assertEquals(".abcd_01234-", new String(ybase64Decoded)); + assertArrayEquals(raw, YBase64.decode(ybase64Decoded)); + } + +}
\ No newline at end of file |