View Javadoc

1   package com.atlassian.vcache.internal.core;
2   
3   import com.google.common.hash.Hashing;
4   
5   import java.nio.charset.Charset;
6   import java.nio.charset.StandardCharsets;
7   import java.util.Base64;
8   
9   /**
10   * Implementation of {@link ExternalCacheKeyGenerator} that encodes using the <tt>SHA-1</tt> hashing algorithm.
11   *
12   * @since 1.0.0
13   */
14  public class Sha1ExternalCacheKeyGenerator extends ExternalCacheKeyGenerator {
15      private static final Charset DIGEST_CHARSET = StandardCharsets.UTF_8;
16  
17      public Sha1ExternalCacheKeyGenerator(String productIdentifier) {
18          super(productIdentifier);
19      }
20  
21      @Override
22      protected String encode(String plain) {
23          /*
24           * We are not using MessageDigest#getInstance because under high load this becomes a contention. This is a known issue in Java
25           * documented here: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=7092821
26           * We are using the Guava implementation instead as they have optimized this bit.
27           */
28          final byte[] digest = Hashing.sha1().hashString(plain, DIGEST_CHARSET).asBytes();
29          return Base64.getEncoder().encodeToString(digest);
30      }
31  }