View Javadoc

1   package com.atlassian.vcache.internal.core;
2   
3   import java.nio.charset.Charset;
4   import java.nio.charset.StandardCharsets;
5   import java.security.MessageDigest;
6   import java.security.NoSuchAlgorithmException;
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 String DIGEST_ALGORITHM = "SHA-1";
16      private static final Charset DIGEST_CHARSET = StandardCharsets.UTF_8;
17  
18      public Sha1ExternalCacheKeyGenerator(String productIdentifier) {
19          super(productIdentifier);
20      }
21  
22      @Override
23      protected String encode(String plain) {
24          try {
25              final MessageDigest md = MessageDigest.getInstance(DIGEST_ALGORITHM);
26              final byte[] digest = md.digest(plain.getBytes(DIGEST_CHARSET));
27              return Base64.getEncoder().encodeToString(digest);
28          } catch (NoSuchAlgorithmException e) {
29              throw new IllegalStateException("Unable to encode to SHA-1", e);
30          }
31      }
32  }