001package org.eclipse.aether.util;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.nio.charset.StandardCharsets;
023import java.security.MessageDigest;
024import java.security.NoSuchAlgorithmException;
025
026/**
027 * A simple digester utility for Strings. Uses {@link MessageDigest} for requested algorithm. Supports one-pass or
028 * several rounds of updates, and as result emits hex encoded String.
029 *
030 * @since 1.9.0
031 */
032public final class StringDigestUtil
033{
034    private final MessageDigest digest;
035
036    /**
037     * Constructs instance with given algorithm.
038     *
039     * @see #sha1()
040     * @see #sha1(String)
041     */
042    public StringDigestUtil( final String alg )
043    {
044        try
045        {
046            this.digest = MessageDigest.getInstance( alg );
047        }
048        catch ( NoSuchAlgorithmException e )
049        {
050            throw new IllegalStateException( "Not supported digest algorithm: " + alg );
051        }
052    }
053
054    /**
055     * Updates instance with passed in string.
056     */
057    public StringDigestUtil update( String data )
058    {
059        if ( data != null && !data.isEmpty() )
060        {
061            digest.update( data.getBytes( StandardCharsets.UTF_8 ) );
062        }
063        return this;
064    }
065
066    /**
067     * Returns the digest of all strings passed via {@link #update(String)} as hex string. There is no state preserved
068     * and due implementation of {@link MessageDigest#digest()}, same applies here: this instance "resets" itself.
069     * Hence, the digest hex encoded string is returned only once.
070     *
071     * @see MessageDigest#digest()
072     */
073    public String digest()
074    {
075        return ChecksumUtils.toHexString( digest.digest() );
076    }
077
078    /**
079     * Helper method to create {@link StringDigestUtil} using SHA-1 digest algorithm.
080     */
081    public static StringDigestUtil sha1()
082    {
083        return new StringDigestUtil( "SHA-1" );
084    }
085
086    /**
087     * Helper method to calculate SHA-1 digest and hex encode it.
088     */
089    public static String sha1( final String string )
090    {
091        return sha1().update( string ).digest();
092    }
093}