aboutsummaryrefslogtreecommitdiffstats
path: root/security-utils/src/main/java/com/yahoo/security/YBase64.java
blob: ab54db8696a8ceec986084ae370bfb0a197e6451 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Copyright Vespa.ai. 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;
    }
}