53 lines
1.8 KiB
Java
53 lines
1.8 KiB
Java
package android.util;
|
|||
|
|
|
||
|
|
import java.nio.charset.StandardCharsets;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* JVM test implementation of android.util.Base64 using java.util.Base64.
|
||
|
|
* Enables running Android SRP cryptographic tests on standard desktop JVM.
|
||
|
|
*/
|
||
|
|
public class Base64 {
|
||
|
|
public static final int DEFAULT = 0;
|
||
|
|
public static final int NO_PADDING = 1;
|
||
|
|
public static final int NO_WRAP = 2;
|
||
|
|
public static final int CRLF = 4;
|
||
|
|
public static final int URL_SAFE = 8;
|
||
|
|
public static final int NO_CLOSE = 16;
|
||
|
|
|
||
|
|
public static String encodeToString(byte[] input, int flags) {
|
||
|
|
return encodeToString(input, 0, input.length, flags);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static String encodeToString(byte[] input, int offset, int len, int flags) {
|
||
|
|
byte[] slice;
|
||
|
|
if (offset == 0 && len == input.length) {
|
||
|
|
slice = input;
|
||
|
|
} else {
|
||
|
|
slice = new byte[len];
|
||
|
|
System.arraycopy(input, offset, slice, 0, len);
|
||
|
|
}
|
||
|
|
java.util.Base64.Encoder encoder = ((flags & URL_SAFE) != 0)
|
||
|
|
? java.util.Base64.getUrlEncoder()
|
||
|
|
: java.util.Base64.getEncoder();
|
||
|
|
if ((flags & NO_PADDING) != 0) {
|
||
|
|
encoder = encoder.withoutPadding();
|
||
|
|
}
|
||
|
|
return encoder.encodeToString(slice);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static byte[] encode(byte[] input, int flags) {
|
||
|
|
return encodeToString(input, flags).getBytes(StandardCharsets.UTF_8);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static byte[] decode(String str, int flags) {
|
||
|
|
java.util.Base64.Decoder decoder = ((flags & URL_SAFE) != 0)
|
||
|
|
? java.util.Base64.getUrlDecoder()
|
||
|
|
: java.util.Base64.getDecoder();
|
||
|
|
return decoder.decode(str);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static byte[] decode(byte[] input, int flags) {
|
||
|
|
return decode(new String(input, StandardCharsets.UTF_8), flags);
|
||
|
|
}
|
||
|
|
}
|