Search in sources :

Example 96 with Hasher

use of com.google.common.hash.Hasher in project java-chassis by ServiceComb.

the class EdgeSignatureRequestFilter method afterReceiveRequest.

@Override
public Response afterReceiveRequest(Invocation invocation, HttpServletRequestEx requestEx) {
    EncryptContext encryptContext = (EncryptContext) invocation.getHandlerContext().get(EdgeConst.ENCRYPT_CONTEXT);
    if (encryptContext == null) {
        return null;
    }
    Hcr hcr = encryptContext.getHcr();
    // signature for query and form
    List<String> names = Collections.list(requestEx.getParameterNames());
    names.sort(Comparator.naturalOrder());
    Hasher hasher = Hashing.sha256().newHasher();
    hasher.putString(hcr.getSignatureKey(), StandardCharsets.UTF_8);
    for (String name : names) {
        hasher.putString(name, StandardCharsets.UTF_8);
        hasher.putString(requestEx.getParameter(name), StandardCharsets.UTF_8);
    }
    LOGGER.info("afterReceiveRequest signature: {}", hasher.hash().toString());
    return null;
}
Also used : Hasher(com.google.common.hash.Hasher) Hcr(org.apache.servicecomb.demo.edge.authentication.encrypt.Hcr) EncryptContext(org.apache.servicecomb.demo.edge.service.encrypt.EncryptContext)

Example 97 with Hasher

use of com.google.common.hash.Hasher in project java-chassis by ServiceComb.

the class SignatureUtils method genSignature.

public static String genSignature(HttpServletRequestEx requestEx) {
    Hasher hasher = Hashing.sha256().newHasher();
    hasher.putString(requestEx.getRequestURI(), StandardCharsets.UTF_8);
    for (String paramName : paramNames) {
        String paramValue = requestEx.getHeader(paramName);
        if (paramValue != null) {
            hasher.putString(paramName, StandardCharsets.UTF_8);
            hasher.putString(paramValue, StandardCharsets.UTF_8);
            System.out.printf("%s %s\n", paramName, paramValue);
        }
    }
    if (!StringUtils.startsWithIgnoreCase(requestEx.getContentType(), MediaType.APPLICATION_FORM_URLENCODED)) {
        byte[] bytes = requestEx.getBodyBytes();
        if (bytes != null) {
            hasher.putBytes(bytes, 0, requestEx.getBodyBytesLength());
        }
    }
    return hasher.hash().toString();
}
Also used : Hasher(com.google.common.hash.Hasher)

Example 98 with Hasher

use of com.google.common.hash.Hasher in project gerrit by GerritCodeReview.

the class RevisionResource method getETag.

@Override
public String getETag() {
    try (TraceTimer ignored = TraceContext.newTimer("Compute revision ETag", Metadata.builder().changeId(change.getId().get()).patchSetId(ps.number()).projectName(change.getProject().get()).build())) {
        Hasher h = Hashing.murmur3_128().newHasher();
        prepareETag(h, getUser());
        return h.hash().toString();
    }
}
Also used : Hasher(com.google.common.hash.Hasher) TraceTimer(com.google.gerrit.server.logging.TraceContext.TraceTimer)

Example 99 with Hasher

use of com.google.common.hash.Hasher in project nzbhydra2 by theotherp.

the class HashPasswordProtector method encryptNow.

private String encryptNow(String plaintext) {
    logger.debug("Encrypt:{}", plaintext);
    byte[] bytes = plaintext.getBytes(Charsets.UTF_8);
    for (int i = 0; i < hashTime - 1; i++) {
        Hasher hasher = chooseHasher(algorithm);
        bytes = hasher.putBytes(bytes).hash().asBytes();
    }
    Hasher hasher = chooseHasher(algorithm);
    return hasher.putBytes(bytes).hash().toString();
}
Also used : Hasher(com.google.common.hash.Hasher)

Example 100 with Hasher

use of com.google.common.hash.Hasher in project druid by druid-io.

the class SegmentUtils method hashIds.

/**
 * Hash the IDs of the given segments based on SHA-256 algorithm.
 */
public static String hashIds(List<DataSegment> segments) {
    Collections.sort(segments);
    final Hasher hasher = HASH_FUNCTION.newHasher();
    segments.forEach(segment -> hasher.putString(segment.getId().toString(), StandardCharsets.UTF_8));
    return StringUtils.fromUtf8(hasher.hash().asBytes());
}
Also used : Hasher(com.google.common.hash.Hasher)

Aggregations

Hasher (com.google.common.hash.Hasher)106 HashCode (com.google.common.hash.HashCode)14 IOException (java.io.IOException)12 Test (org.junit.Test)10 HashFunction (com.google.common.hash.HashFunction)6 InputStream (java.io.InputStream)6 Map (java.util.Map)5 BuildTarget (com.facebook.buck.model.BuildTarget)4 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)4 SettableFakeClock (com.facebook.buck.timing.SettableFakeClock)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 Hcr (org.apache.servicecomb.demo.edge.authentication.encrypt.Hcr)4 EncryptContext (org.apache.servicecomb.demo.edge.service.encrypt.EncryptContext)4 Hcr (org.apache.servicecomb.it.authentication.encrypt.Hcr)4 EncryptContext (org.apache.servicecomb.it.edge.encrypt.EncryptContext)4 Buffer (io.vertx.core.buffer.Buffer)3 OutputStream (java.io.OutputStream)3 Path (java.nio.file.Path)3 StringUtils.byteToHexString (org.apache.flink.util.StringUtils.byteToHexString)3 EObject (org.eclipse.emf.ecore.EObject)3