spring security Sha512DigestUtils 源码

  • 2022-08-13
  • 浏览 (315)

spring security Sha512DigestUtils 代码

文件路径:/core/src/main/java/org/springframework/security/core/token/Sha512DigestUtils.java

/*
 * Copyright 2002-2016 the original author or authors.
 *
 * 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
 *
 *      https://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.springframework.security.core.token;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.springframework.security.crypto.codec.Hex;

/**
 * Provides SHA512 digest methods.
 *
 * <p>
 * Based on Commons Codec, which does not presently provide SHA512 support.
 * </p>
 *
 * @author Ben Alex
 * @since 2.0.1
 *
 */
public abstract class Sha512DigestUtils {

	/**
	 * Returns an SHA digest.
	 * @return An SHA digest instance.
	 * @throws RuntimeException when a {@link java.security.NoSuchAlgorithmException} is
	 * caught.
	 */
	private static MessageDigest getSha512Digest() {
		try {
			return MessageDigest.getInstance("SHA-512");
		}
		catch (NoSuchAlgorithmException ex) {
			throw new RuntimeException(ex.getMessage());
		}
	}

	/**
	 * Calculates the SHA digest and returns the value as a <code>byte[]</code>.
	 * @param data Data to digest
	 * @return SHA digest
	 */
	public static byte[] sha(byte[] data) {
		return getSha512Digest().digest(data);
	}

	/**
	 * Calculates the SHA digest and returns the value as a <code>byte[]</code>.
	 * @param data Data to digest
	 * @return SHA digest
	 */
	public static byte[] sha(String data) {
		return sha(data.getBytes());
	}

	/**
	 * Calculates the SHA digest and returns the value as a hex string.
	 * @param data Data to digest
	 * @return SHA digest as a hex string
	 */
	public static String shaHex(byte[] data) {
		return new String(Hex.encode(sha(data)));
	}

	/**
	 * Calculates the SHA digest and returns the value as a hex string.
	 * @param data Data to digest
	 * @return SHA digest as a hex string
	 */
	public static String shaHex(String data) {
		return new String(Hex.encode(sha(data)));
	}

}

相关信息

spring security 源码目录

相关文章

spring security DefaultToken 源码

spring security KeyBasedPersistenceTokenService 源码

spring security SecureRandomFactoryBean 源码

spring security Token 源码

spring security TokenService 源码

spring security package-info 源码

0  赞