All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.omnifaces.utils.security.MessageDigests Maven / Gradle / Ivy

There is a newer version: 0.14
Show newest version
/*
 * Copyright 2018 OmniFaces
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package org.omnifaces.utils.security;

import static java.nio.charset.StandardCharsets.UTF_8;

import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public final class MessageDigests {

	private MessageDigests() {
	}

	/**
	 * Returns a {@link MessageDigest} instance that implements the specified digest algorithm.
	 *
	 * 

* This method calls {@link MessageDigest#getInstance(String)}, but wraps any potential {@link NoSuchAlgorithmException}s in a * {@link UncheckedNoSuchAlgorithmException} as this is an unrecoverable problem in most cases. *

* * @param algorithm * the name of the algorithm to use * @return a {@link MessageDigest} instance that implements the specified algorithm * @throws UncheckedNoSuchAlgorithmException * if no implementation of the given algorithm is found */ public static MessageDigest getMessageDigestInstance(String algorithm) throws UncheckedNoSuchAlgorithmException { try { return MessageDigest.getInstance(algorithm); } catch (NoSuchAlgorithmException e) { throw new UncheckedNoSuchAlgorithmException(e); } } /** * Calculate a message digest over a given string using the specified algorithm. * * This method will use {@link java.nio.charset.StandardCharsets#UTF_8 UTF_8} encoding. * * @param message * the message to calculate the digest over * @param algorithm * the name of the algorithm * @return a byte array containing the message digest * @throws UncheckedNoSuchAlgorithmException * if no implementation of the given algorithm could be found */ public static byte[] digest(String message, String algorithm) throws UncheckedNoSuchAlgorithmException { return digest(message, UTF_8, algorithm); } public static byte[] digest(String message, Charset charset, String algorithm) throws UncheckedNoSuchAlgorithmException { return digest(message.getBytes(charset), algorithm); } public static byte[] digest(String message, byte[] salt, String algorithm) throws UncheckedNoSuchAlgorithmException { return digest(message, UTF_8, salt, algorithm); } public static byte[] digest(String message, Charset charset, byte[] salt, String algorithm) throws UncheckedNoSuchAlgorithmException { return digest(message.getBytes(charset), salt, algorithm); } public static byte[] digest(byte[] message, String algorithm) throws UncheckedNoSuchAlgorithmException { return getMessageDigestInstance(algorithm).digest(message); } public static byte[] digest(byte[] message, byte[] salt, String algorithm) throws UncheckedNoSuchAlgorithmException { MessageDigest messageDigest = getMessageDigestInstance(algorithm); messageDigest.update(salt); return messageDigest.digest(message); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy