View Javadoc
1   package org.eclipse.aether.internal.impl.checksum;
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 org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithm;
23  import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
24  import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactorySupport;
25  import org.eclipse.aether.util.ChecksumUtils;
26  
27  import java.nio.ByteBuffer;
28  import java.security.MessageDigest;
29  import java.security.NoSuchAlgorithmException;
30  
31  /**
32   * Support class to implement {@link ChecksumAlgorithmFactory} based on Java {@link MessageDigest}.
33   *
34   * @since 1.8.0
35   */
36  public abstract class MessageDigestChecksumAlgorithmFactorySupport
37          extends ChecksumAlgorithmFactorySupport
38  {
39      public MessageDigestChecksumAlgorithmFactorySupport( String name, String extension )
40      {
41          super( name, extension );
42      }
43  
44      @Override
45      public ChecksumAlgorithm getAlgorithm()
46      {
47          try
48          {
49              MessageDigest messageDigest = MessageDigest.getInstance( getName() );
50              return new ChecksumAlgorithm()
51              {
52                  @Override
53                  public void update( final ByteBuffer input )
54                  {
55                      messageDigest.update( input );
56                  }
57  
58                  @Override
59                  public String checksum()
60                  {
61                      return ChecksumUtils.toHexString( messageDigest.digest() );
62                  }
63              };
64          }
65          catch ( NoSuchAlgorithmException e )
66          {
67              throw new IllegalStateException(
68                      "MessageDigest algorithm " + getName() + " not supported, but is required by resolver.", e );
69          }
70      }
71  }