001/* 002 * Copyright 2015-2024 the original author or authors 003 * 004 * This software is licensed under the Apache License, Version 2.0, 005 * the GNU Lesser General Public License version 2 or later ("LGPL") 006 * and the WTFPL. 007 * You may choose either license to govern your use of this software only 008 * upon the condition that you accept all of the terms of either 009 * the Apache License 2.0, the LGPL 2.1+ or the WTFPL. 010 */ 011package org.minidns.dnssec.algorithms; 012 013import org.minidns.dnssec.DigestCalculator; 014 015import java.security.MessageDigest; 016import java.security.NoSuchAlgorithmException; 017 018/** 019 * A {@link DigestCalculator} using {@link MessageDigest}. 020 * <p> 021 * Instances are shared, e.g. via {@link AlgorithmMap#INSTANCE}, and hence used concurrently. Since {@link MessageDigest} 022 * is not thread-safe, access to it is synchronized. 023 * </p> 024 */ 025public class JavaSecDigestCalculator implements DigestCalculator { 026 private final MessageDigest md; 027 028 public JavaSecDigestCalculator(String algorithm) throws NoSuchAlgorithmException { 029 md = MessageDigest.getInstance(algorithm); 030 } 031 032 @Override 033 public synchronized byte[] digest(byte[] bytes) { 034 return md.digest(bytes); 035 } 036}