View Javadoc
1   package org.eclipse.aether.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.nio.charset.StandardCharsets;
23  import java.security.MessageDigest;
24  import java.security.NoSuchAlgorithmException;
25  
26  /**
27   * A simple digester utility for Strings. Uses {@link MessageDigest} for requested algorithm. Supports one-pass or
28   * several rounds of updates, and as result emits hex encoded String.
29   *
30   * @since 1.9.0
31   */
32  public final class StringDigestUtil
33  {
34      private final MessageDigest digest;
35  
36      /**
37       * Constructs instance with given algorithm.
38       *
39       * @see #sha1()
40       * @see #sha1(String)
41       */
42      public StringDigestUtil( final String alg )
43      {
44          try
45          {
46              this.digest = MessageDigest.getInstance( alg );
47          }
48          catch ( NoSuchAlgorithmException e )
49          {
50              throw new IllegalStateException( "Not supported digest algorithm: " + alg );
51          }
52      }
53  
54      /**
55       * Updates instance with passed in string.
56       */
57      public StringDigestUtil update( String data )
58      {
59          if ( data != null && !data.isEmpty() )
60          {
61              digest.update( data.getBytes( StandardCharsets.UTF_8 ) );
62          }
63          return this;
64      }
65  
66      /**
67       * Returns the digest of all strings passed via {@link #update(String)} as hex string. There is no state preserved
68       * and due implementation of {@link MessageDigest#digest()}, same applies here: this instance "resets" itself.
69       * Hence, the digest hex encoded string is returned only once.
70       *
71       * @see MessageDigest#digest()
72       */
73      public String digest()
74      {
75          return ChecksumUtils.toHexString( digest.digest() );
76      }
77  
78      /**
79       * Helper method to create {@link StringDigestUtil} using SHA-1 digest algorithm.
80       */
81      public static StringDigestUtil sha1()
82      {
83          return new StringDigestUtil( "SHA-1" );
84      }
85  
86      /**
87       * Helper method to calculate SHA-1 digest and hex encode it.
88       */
89      public static String sha1( final String string )
90      {
91          return sha1().update( string ).digest();
92      }
93  }