List of usage examples for com.google.common.io BaseEncoding encode
public String encode(byte[] bytes)
From source file:org.apache.gobblin.util.SerializationUtils.java
/** * Serialize an object into a String. The object is first serialized into a byte array, * which is converted into a String using the given {@link BaseEncoding}. * * @param obj A {@link Serializable} object * @param enc The {@link BaseEncoding} used to encode a byte array. * @return A String representing the input object * @throws IOException if it fails to serialize the object *//*ww w.ja v a2 s . c om*/ public static <T extends Serializable> String serialize(T obj, BaseEncoding enc) throws IOException { return enc.encode(serializeIntoBytes(obj)); }
From source file:org.sfs.validate.HttpRequestExceptionValidations.java
private static void validateHeader(String headerName, byte[] expectedMd5, byte[] calculatedMd5, BaseEncoding baseEncoding) { if (!Arrays.equals(calculatedMd5, expectedMd5)) { JsonObject jsonObject = new JsonObject().put("message", format("%s was %s, expected %s", headerName, baseEncoding.encode(calculatedMd5), baseEncoding.encode(expectedMd5))); throw new HttpRequestValidationException(HTTP_CONFLICT, jsonObject); }/*from w ww . j av a 2 s .c om*/ }
From source file:org.tc33.jenigma.test.CookieEncrypt.java
public static String customEncrypt(String content, String password) { times += 1;//from w w w . j a v a 2s. c o m plainLength += content.length(); if (password.length() != 8) { return null; } int blockSize = 4; byte[] dataBytes = content.getBytes(); int plaintextLength = dataBytes.length; if (plaintextLength % blockSize != 0) { plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize)); } byte[] plaintext = new byte[plaintextLength]; System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length); for (int i = 0; i < plaintextLength; i++) { if (i % 2 == 0) { plaintext[i] ^= i; } else { plaintext[i] ^= (plaintextLength - i); } plaintext[i] = bswap(plaintext[i]); } //System.out.println(Hex.toHexString(plaintext)); int salt = 385751695; byte[] key1 = new byte[4]; System.arraycopy(password.getBytes(), 0, key1, 0, 4); key1 = int2byte(byte2int(key1) ^ salt++); byte[] key2 = new byte[4]; System.arraycopy(password.getBytes(), 4, key2, 0, 4); key2 = int2byte(byte2int(key2) ^ salt++); key1[0] = (byte) (key1[0] ^ key2[0] ^ key2[1]); key1[1] = (byte) (key1[1] ^ key2[0] ^ key2[2]); key1[2] = (byte) (key1[2] ^ key2[2] ^ key2[3]); key1[3] = (byte) (key1[3] ^ key2[1] ^ key2[3]); //System.out.println(Hex.toHexString(key1)); //System.out.println(Hex.toHexString(key2)); int k = 0; byte[] output = new byte[plaintextLength]; while (k < plaintext.length / 4) { if (k % 2 == 0) { byte[] tmp1 = new byte[4]; for (int j = 0; j < 4; j++) { tmp1[j] = (byte) (key1[j] ^ plaintext[4 * k + j]); } System.arraycopy(tmp1, 0, output, 4 * k, 4); } else { byte[] tmp2 = new byte[4]; for (int j = 0; j < 4; j++) { tmp2[j] = (byte) (key1[j] ^ key2[j] ^ plaintext[4 * k + j]); } System.arraycopy(tmp2, 0, output, 4 * k, 4); } k++; } //System.out.println(Hex.toHexString(output)); BaseEncoding b64 = BaseEncoding.base64Url().omitPadding(); String cipher_text = b64.encode(output); ciperLength += cipher_text.length(); return cipher_text; }
From source file:com.foundationdb.util.Strings.java
public static String formatMD5(byte[] md5, boolean toLowerCase) { BaseEncoding encoder = toLowerCase ? BaseEncoding.base16().lowerCase() : BaseEncoding.base16().upperCase(); return encoder.encode(md5); }
From source file:com.google.cloud.storage.spi.DefaultStorageRpc.java
private static void setEncryptionHeaders(HttpHeaders headers, String headerPrefix, Map<Option, ?> options) { String key = CUSTOMER_SUPPLIED_KEY.getString(options); if (key != null) { BaseEncoding base64 = BaseEncoding.base64(); HashFunction hashFunction = Hashing.sha256(); headers.set(headerPrefix + "algorithm", "AES256"); headers.set(headerPrefix + "key", key); headers.set(headerPrefix + "key-sha256", base64.encode(hashFunction.hashBytes(base64.decode(key)).asBytes())); }/* w w w . j av a 2 s . c o m*/ }
From source file:com.google.cloud.storage.spi.v1.HttpStorageRpc.java
private static void setEncryptionHeaders(HttpHeaders headers, String headerPrefix, Map<Option, ?> options) { String key = Option.CUSTOMER_SUPPLIED_KEY.getString(options); if (key != null) { BaseEncoding base64 = BaseEncoding.base64(); HashFunction hashFunction = Hashing.sha256(); headers.set(headerPrefix + "algorithm", "AES256"); headers.set(headerPrefix + "key", key); headers.set(headerPrefix + "key-sha256", base64.encode(hashFunction.hashBytes(base64.decode(key)).asBytes())); }/*from ww w. ja va 2 s.c o m*/ }
From source file:org.sfs.validate.ValidateParamIsBase64Encoded.java
@Override public SfsRequest call(SfsRequest httpServerRequest) { MultiMap params = httpServerRequest.params(); String value = params.get(paramName); if (value != null) { boolean failed = false; BaseEncoding baseEncoding = base64(); try {//from w ww . j a va 2 s . c om byte[] decoded = baseEncoding.decode(value); if (decoded == null || value.equals(baseEncoding.encode(decoded))) { failed = true; } } catch (Throwable e) { // do nothing } if (failed) { JsonObject jsonObject = new JsonObject().put("message", format("%s must be Base64 encoded", paramName)); throw new HttpRequestValidationException(HTTP_BAD_REQUEST, jsonObject); } } return httpServerRequest; }
From source file:org.sfs.validate.ValidateHeaderIsBase64Encoded.java
@Override public SfsRequest call(SfsRequest httpServerRequest) { MultiMap headers = httpServerRequest.headers(); String value = headers.get(headerName); if (value != null) { boolean failed = false; BaseEncoding baseEncoding = base64(); try {/* ww w . j ava 2 s .co m*/ byte[] decoded = baseEncoding.decode(value); if (decoded == null || !value.equals(baseEncoding.encode(decoded))) { failed = true; } } catch (Throwable e) { // do nothing } if (failed) { JsonObject jsonObject = new JsonObject().put("message", format("%s must be Base64 encoded", headerName)); throw new HttpRequestValidationException(HTTP_BAD_REQUEST, jsonObject); } } return httpServerRequest; }
From source file:com.example.appengine.firetactoe.FirebaseChannel.java
/** * Create a secure JWT token for the given userId. *//*w w w . j a va 2s.c o m*/ public String createFirebaseToken(Game game, String userId) { final AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService(); final BaseEncoding base64 = BaseEncoding.base64(); String header = base64.encode("{\"typ\":\"JWT\",\"alg\":\"RS256\"}".getBytes()); // Construct the claim String channelKey = game.getChannelKey(userId); String clientEmail = appIdentity.getServiceAccountName(); long epochTime = System.currentTimeMillis() / 1000; long expire = epochTime + 60 * 60; // an hour from now Map<String, Object> claims = new HashMap<String, Object>(); claims.put("iss", clientEmail); claims.put("sub", clientEmail); claims.put("aud", IDENTITY_ENDPOINT); claims.put("uid", channelKey); claims.put("iat", epochTime); claims.put("exp", expire); String payload = base64.encode(new Gson().toJson(claims).getBytes()); String toSign = String.format("%s.%s", header, payload); AppIdentityService.SigningResult result = appIdentity.signForApp(toSign.getBytes()); return String.format("%s.%s", toSign, base64.encode(result.getSignature())); }
From source file:org.sfs.validate.ValidateHeaderIsBase16LowercaseEncoded.java
@Override public SfsRequest call(SfsRequest httpServerRequest) { MultiMap headers = httpServerRequest.headers(); String value = headers.get(headerName); if (value != null) { boolean failed = false; BaseEncoding baseEncoding = base16().lowerCase(); try {// w ww . j a va 2s . co m byte[] decoded = baseEncoding.decode(value); if (decoded == null) { failed = true; } else { String encoded = baseEncoding.encode(decoded); if (!value.equals(encoded)) { failed = true; } } } catch (Throwable e) { // do nothing } if (failed) { JsonObject jsonObject = new JsonObject().put("message", format("%s must be Base16 lowercase encoded", headerName)); throw new HttpRequestValidationException(HTTP_BAD_REQUEST, jsonObject); } } return httpServerRequest; }