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