summaryrefslogtreecommitdiffstats
path: root/security-utils/src/main/java/com/yahoo/security/Base62.java
diff options
context:
space:
mode:
authorTor Brede Vekterli <vekterli@yahooinc.com>2022-11-08 13:10:23 +0100
committerTor Brede Vekterli <vekterli@yahooinc.com>2022-11-08 13:42:00 +0100
commitfc1c56f2e080a35bbc34a27d2cabc53f8ea171b0 (patch)
tree21d96d4c17ef656c9af5df48a279abc1faf61312 /security-utils/src/main/java/com/yahoo/security/Base62.java
parent247657a8e00c05b5ff3fe9d15ca258f41cedd597 (diff)
Add a codec that enables conversion to and from a base N representation
Adds a codec that enables easy conversion from an array of bytes to any numeric base in [2, 256) and back again, using a supplied custom alphabet. Implemented by treating the input byte sequence to encode verbatim as a big-endian `BigInteger` and iteratively doing a `divmod` operation until the quotient is zero, emitting the modulus mapped onto the alphabet for each iteration. Decoding reverses this process, ending up with the same `BigInteger` as in the initial encoding step.
Diffstat (limited to 'security-utils/src/main/java/com/yahoo/security/Base62.java')
-rw-r--r--security-utils/src/main/java/com/yahoo/security/Base62.java21
1 files changed, 21 insertions, 0 deletions
diff --git a/security-utils/src/main/java/com/yahoo/security/Base62.java b/security-utils/src/main/java/com/yahoo/security/Base62.java
new file mode 100644
index 00000000000..86c60a1bb1d
--- /dev/null
+++ b/security-utils/src/main/java/com/yahoo/security/Base62.java
@@ -0,0 +1,21 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.security;
+
+/**
+ * Base62 encoding which has the nice property that it does not feature any
+ * potential word/line-breaking characters, which means encoded strings can
+ * usually be selected in one go on web pages or in the terminal.
+ *
+ * @see <a href="https://en.wikipedia.org/wiki/Base62">Base62 on Wiki</a>
+ *
+ * @author vekterli
+ */
+public class Base62 {
+
+ private static final BaseNCodec INSTANCE = BaseNCodec.of("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
+
+ public static BaseNCodec codec() {
+ return INSTANCE;
+ }
+
+}