Java Code Examples for com.google.common.io.BaseEncoding#encode()

The following examples show how to use com.google.common.io.BaseEncoding#encode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FirebaseChannel.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
/** Create a secure JWT token for the given userId. */
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()));
}
 
Example 2
Source File: compact_signature.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
@Override
public JsonElement serialize(compact_signature src,
                             Type typeOfSrc,
                             JsonSerializationContext context) {
    BaseEncoding encoding = BaseEncoding.base16().lowerCase();

    return new JsonPrimitive(encoding.encode(src.data));
}
 
Example 3
Source File: AbstractTransaction.java    From java-stellar-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Returns base64-encoded TransactionEnvelope XDR object. Transaction need to have at least one signature.
 */
public String toEnvelopeXdrBase64() {
  try {
    TransactionEnvelope envelope = this.toEnvelopeXdr();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    XdrDataOutputStream xdrOutputStream = new XdrDataOutputStream(outputStream);
    TransactionEnvelope.encode(xdrOutputStream, envelope);

    BaseEncoding base64Encoding = BaseEncoding.base64();
    return base64Encoding.encode(outputStream.toByteArray());
  } catch (IOException e) {
    throw new AssertionError(e);
  }
}
 
Example 4
Source File: ValidateHeaderIsBase16LowercaseEncoded.java    From sfs with Apache License 2.0 5 votes vote down vote up
@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 {
            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;
}
 
Example 5
Source File: gson_common_serializer.java    From bitshares_wallet with MIT License 5 votes vote down vote up
@Override
public JsonElement serialize(ByteBuffer src,
                             Type typeOfSrc,
                             JsonSerializationContext context) {
    BaseEncoding encoding = BaseEncoding.base16().lowerCase();

    return new JsonPrimitive(encoding.encode(src.array()));
}
 
Example 6
Source File: compact_signature.java    From bitshares_wallet with MIT License 5 votes vote down vote up
@Override
public JsonElement serialize(compact_signature src,
                             Type typeOfSrc,
                             JsonSerializationContext context) {
    BaseEncoding encoding = BaseEncoding.base16().lowerCase();

    return new JsonPrimitive(encoding.encode(src.data));
}
 
Example 7
Source File: sha256_object.java    From bitshares_wallet with MIT License 5 votes vote down vote up
@Override
public JsonElement serialize(sha256_object src,
                             Type typeOfSrc,
                             JsonSerializationContext context) {

    BaseEncoding encoding = BaseEncoding.base16().lowerCase();

    return new JsonPrimitive(encoding.encode(src.hash));
}
 
Example 8
Source File: gson_common_serializer.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
@Override
public JsonElement serialize(ByteBuffer src,
                             Type typeOfSrc,
                             JsonSerializationContext context) {
    BaseEncoding encoding = BaseEncoding.base16().lowerCase();

    return new JsonPrimitive(encoding.encode(src.array()));
}
 
Example 9
Source File: sha256_object.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
@Override
public JsonElement serialize(sha256_object src,
                             Type typeOfSrc,
                             JsonSerializationContext context) {

    BaseEncoding encoding = BaseEncoding.base16().lowerCase();

    return new JsonPrimitive(encoding.encode(src.hash));
}
 
Example 10
Source File: compact_signature.java    From AndroidWallet with GNU General Public License v3.0 5 votes vote down vote up
@Override
public JsonElement serialize(compact_signature src, Type typeOfSrc,
                             JsonSerializationContext context) {
    BaseEncoding encoding = BaseEncoding.base16().lowerCase();

    return new JsonPrimitive(encoding.encode(src.data));
}
 
Example 11
Source File: TransactionDecodeTest.java    From java-stellar-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void testRoundtrip() throws IOException {
    String txBody = "AAAAAM6jLgjKjuXxWkir4M7v0NqoOfODXcFnn6AGlP+d4RxAAAAAZAAIiE4AAAABAAAAAAAAAAEAAAAcyKMl+WDSzuttWkF2DvzKAkkEqeSZ4cZihjGJEAAAAAEAAAAAAAAAAQAAAAAgECmBaDwiRPE1z2vAE36J+45toU/ZxdvpR38tc0HvmgAAAAAAAAAAAJiWgAAAAAAAAAABneEcQAAAAECeXDKebJoAbST1T2AbDBui9K0TbSM8sfbhXUAZ2ROAoCRs5cG1pRvY+ityyPWFEKPd7+3qEupavkAZ/+L7/28G";
    BaseEncoding base64Encoding = BaseEncoding.base64();
    byte[] bytes = base64Encoding.decode(txBody);

    TransactionEnvelope transactionEnvelope = TransactionEnvelope.decode(new XdrDataInputStream(new ByteArrayInputStream(bytes)));
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();

    transactionEnvelope.encode(new XdrDataOutputStream(byteOutputStream));
    String serialized = base64Encoding.encode(byteOutputStream.toByteArray());
    assertEquals(serialized, txBody);
}
 
Example 12
Source File: sha256_object.java    From AndroidWallet with GNU General Public License v3.0 5 votes vote down vote up
@Override
public JsonElement serialize(sha256_object src,
                             Type typeOfSrc,
                             JsonSerializationContext context) {

    BaseEncoding encoding = BaseEncoding.base16().lowerCase();

    return new JsonPrimitive(encoding.encode(src.hash));
}
 
Example 13
Source File: sha224_object.java    From guarda-android-wallets with GNU General Public License v3.0 4 votes vote down vote up
@Override
public String toString() {
    BaseEncoding encoding = BaseEncoding.base16().lowerCase();
    return encoding.encode(hash);
}
 
Example 14
Source File: sha512_object.java    From AndroidWallet with GNU General Public License v3.0 4 votes vote down vote up
@Override
public String toString() {
    BaseEncoding encoding = BaseEncoding.base16().lowerCase();
    return encoding.encode(hash);
}
 
Example 15
Source File: sha256_object.java    From guarda-android-wallets with GNU General Public License v3.0 4 votes vote down vote up
@Override
public String toString() {
    BaseEncoding encoding = BaseEncoding.base16().lowerCase();
    return encoding.encode(hash);
}
 
Example 16
Source File: sha512_object.java    From guarda-android-wallets with GNU General Public License v3.0 4 votes vote down vote up
@Override
public String toString() {
    BaseEncoding encoding = BaseEncoding.base16().lowerCase();
    return encoding.encode(hash);
}
 
Example 17
Source File: sha256_object.java    From bitshares_wallet with MIT License 4 votes vote down vote up
@Override
public String toString() {
    BaseEncoding encoding = BaseEncoding.base16().lowerCase();
    return encoding.encode(hash);
}
 
Example 18
Source File: sha224_object.java    From bitshares_wallet with MIT License 4 votes vote down vote up
@Override
public String toString() {
    BaseEncoding encoding = BaseEncoding.base16().lowerCase();
    return encoding.encode(hash);
}
 
Example 19
Source File: sha256_object.java    From AndroidWallet with GNU General Public License v3.0 4 votes vote down vote up
@Override
public String toString() {
    BaseEncoding encoding = BaseEncoding.base16().lowerCase();
    return encoding.encode(hash);
}
 
Example 20
Source File: SerializationUtils.java    From incubator-gobblin with Apache License 2.0 2 votes vote down vote up
/**
 * 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
 */
public static <T extends Serializable> String serialize(T obj, BaseEncoding enc) throws IOException {
  return enc.encode(serializeIntoBytes(obj));
}