use of com.google.common.io.BaseEncoding in project habot by ghys.
the class PushService method send.
/**
* Send a notification and wait for the response.
*
* @param notification
* @return
* @throws GeneralSecurityException
* @throws IOException
* @throws JoseException
* @throws ExecutionException
* @throws InterruptedException
*/
public Future<Response> send(Notification notification) throws GeneralSecurityException, IOException, JoseException, ExecutionException, InterruptedException {
assert (verifyKeyPair());
BaseEncoding base64url = BaseEncoding.base64Url();
Encrypted encrypted = encrypt(notification.getPayload(), notification.getUserPublicKey(), notification.getUserAuth(), notification.getPadSize());
byte[] dh = Utils.savePublicKey((ECPublicKey) encrypted.getPublicKey());
byte[] salt = encrypted.getSalt();
Invocation.Builder invocationBuilder = ClientBuilder.newClient().target(notification.getEndpoint()).request();
MultivaluedMap<String, Object> headers = new MultivaluedHashMap<String, Object>();
headers.add("TTL", String.valueOf(notification.getTTL()));
if (notification.hasPayload()) {
headers.add("Content-Type", "application/octet-stream");
headers.add("Content-Encoding", "aesgcm");
headers.add("Encryption", "keyid=p256dh;salt=" + base64url.omitPadding().encode(salt));
headers.add("Crypto-Key", "keyid=p256dh;dh=" + base64url.encode(dh));
}
if (notification.isGcm()) {
if (gcmApiKey == null) {
throw new IllegalStateException("An GCM API key is needed to send a push notification to a GCM endpoint.");
}
headers.add("Authorization", "key=" + gcmApiKey);
}
if (vapidEnabled() && !notification.isGcm()) {
JwtClaims claims = new JwtClaims();
claims.setAudience(notification.getOrigin());
claims.setExpirationTimeMinutesInTheFuture(12 * 60);
claims.setSubject(subject);
JsonWebSignature jws = new JsonWebSignature();
jws.setHeader("typ", "JWT");
jws.setHeader("alg", "ES256");
jws.setPayload(claims.toJson());
jws.setKey(privateKey);
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.ECDSA_USING_P256_CURVE_AND_SHA256);
headers.add("Authorization", "WebPush " + jws.getCompactSerialization());
byte[] pk = Utils.savePublicKey((ECPublicKey) publicKey);
if (headers.containsKey("Crypto-Key")) {
headers.add("Crypto-Key", headers.get("Crypto-Key") + ";p256ecdsa=" + base64url.omitPadding().encode(pk));
} else {
headers.add("Crypto-Key", "p256ecdsa=" + base64url.encode(pk));
}
}
invocationBuilder.headers(headers);
if (notification.hasPayload()) {
return invocationBuilder.async().post(Entity.entity(encrypted.getCiphertext(), new Variant(MediaType.APPLICATION_OCTET_STREAM_TYPE, (String) null, "aesgcm")));
} else {
return invocationBuilder.async().post(null);
}
}
use of com.google.common.io.BaseEncoding in project webprotege by protegeproject.
the class SaltReadConverter method convert.
@Override
public Salt convert(String s) {
BaseEncoding encoding = BaseEncoding.base16();
byte[] bytes = encoding.decode(s.toUpperCase(Locale.ENGLISH));
return new Salt(bytes);
}
use of com.google.common.io.BaseEncoding in project OpenTripPlanner by opentripplanner.
the class TripPattern method semanticHashString.
/**
* In most cases we want to use identity equality for Trips.
* However, in some cases we want a way to consistently identify trips across versions of a GTFS feed, when the
* feed publisher cannot ensure stable trip IDs. Therefore we define some additional hash functions.
* Hash collisions are theoretically possible, so these identifiers should only be used to detect when two
* trips are the same with a high degree of probability.
* An example application is avoiding double-booking of a particular bus trip for school field trips.
* Using Murmur hash function. see http://programmers.stackexchange.com/a/145633 for comparison.
*
* @param trip a trip object within this pattern, or null to hash the pattern itself independent any specific trip.
* @return the semantic hash of a Trip in this pattern as a printable String.
*
* TODO deal with frequency-based trips
*/
public String semanticHashString(Trip trip) {
HashFunction murmur = Hashing.murmur3_32();
BaseEncoding encoder = BaseEncoding.base64Url().omitPadding();
StringBuilder sb = new StringBuilder(50);
sb.append(encoder.encode(stopPattern.semanticHash(murmur).asBytes()));
if (trip != null) {
TripTimes tripTimes = scheduledTimetable.getTripTimes(trip);
if (tripTimes == null)
return null;
sb.append(':');
sb.append(encoder.encode(tripTimes.semanticHash(murmur).asBytes()));
}
return sb.toString();
}
use of com.google.common.io.BaseEncoding in project graylog2-server by Graylog2.
the class Base16Encode method getEncodedValue.
@Override
protected String getEncodedValue(String value, boolean omitPadding) {
BaseEncoding encoding = BaseEncoding.base16();
encoding = omitPadding ? encoding.omitPadding() : encoding;
return encoding.encode(value.getBytes(StandardCharsets.UTF_8));
}
use of com.google.common.io.BaseEncoding in project graylog2-server by Graylog2.
the class Base16Decode method getEncodedValue.
@Override
protected String getEncodedValue(String value, boolean omitPadding) {
BaseEncoding encoding = BaseEncoding.base16();
encoding = omitPadding ? encoding.omitPadding() : encoding;
return new String(encoding.decode(value), StandardCharsets.UTF_8);
}
Aggregations