Search in sources :

Example 16 with BaseEncoding

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);
    }
}
Also used : Invocation(javax.ws.rs.client.Invocation) JwtClaims(org.jose4j.jwt.JwtClaims) BaseEncoding(com.google.common.io.BaseEncoding) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Variant(javax.ws.rs.core.Variant) JsonWebSignature(org.jose4j.jws.JsonWebSignature)

Example 17 with BaseEncoding

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);
}
Also used : Salt(edu.stanford.bmir.protege.web.shared.auth.Salt) BaseEncoding(com.google.common.io.BaseEncoding)

Example 18 with BaseEncoding

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();
}
Also used : HashFunction(com.google.common.hash.HashFunction) TripTimes(org.opentripplanner.routing.trippattern.TripTimes) BaseEncoding(com.google.common.io.BaseEncoding)

Example 19 with BaseEncoding

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));
}
Also used : BaseEncoding(com.google.common.io.BaseEncoding)

Example 20 with BaseEncoding

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);
}
Also used : BaseEncoding(com.google.common.io.BaseEncoding)

Aggregations

BaseEncoding (com.google.common.io.BaseEncoding)21 HashFunction (com.google.common.hash.HashFunction)3 IOException (java.io.IOException)2 Test (org.junit.Test)2 GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)1 GenericUrl (com.google.api.client.http.GenericUrl)1 HttpHeaders (com.google.api.client.http.HttpHeaders)1 HttpRequest (com.google.api.client.http.HttpRequest)1 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)1 HttpResponse (com.google.api.client.http.HttpResponse)1 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)1 JsonHttpContent (com.google.api.client.http.json.JsonHttpContent)1 JsonFactory (com.google.api.client.json.JsonFactory)1 Insert (com.google.api.services.storage.Storage.Objects.Insert)1 StorageObject (com.google.api.services.storage.model.StorageObject)1 AppIdentityService (com.google.appengine.api.appidentity.AppIdentityService)1 Artifact (com.google.devtools.build.lib.actions.Artifact)1 TreeFileArtifact (com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact)1 Gson (com.google.gson.Gson)1 Salt (edu.stanford.bmir.protege.web.shared.auth.Salt)1