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

org.nervousync.security.digest.BaseDigestAdapter Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
/*
 * Licensed to the Nervousync Studio (NSYC) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.nervousync.security.digest;

import org.bouncycastle.crypto.Mac;
import org.nervousync.commons.Globals;
import org.nervousync.security.api.SecureAdapter;
import org.nervousync.exceptions.crypto.CryptoException;
import org.nervousync.utils.StringUtils;

import java.security.MessageDigest;
import java.util.Arrays;

/**
 * 

Abstract basic digest adapter class

*

摘要算法适配器的抽象类

* * @author Steven Wee [email protected] * @version $Revision: 1.0.0 $ $Date: Jan 13, 2012 11:46:55 $ */ public abstract class BaseDigestAdapter extends SecureAdapter { /** * Hash-based Message Authentication Code * 基于密钥的消息认证码算法 */ private final boolean macMode; /** * MessageDigest instance * 消息摘要算法实例对象 */ private final MessageDigest messageDigest; /** * Message Authentication Code instance * 消息认证码算法实例对象 */ private final Mac hmac; /** *

Constructor for BaseDigestAdapter

*

消息摘要算法适配器的构造方法

* * @param algorithm Cipher Algorithm * 密码算法 * @param keyBytes Hmac key data bytes * 消息认证码算法密钥数据数组 * * @throws CryptoException * If an error occurs when initialize adaptor * 当初始化适配器时出现异常 */ protected BaseDigestAdapter(final String algorithm, final byte[] keyBytes) throws CryptoException { if (StringUtils.isEmpty(algorithm)) { throw new CryptoException(0x00000015000DL, "Unknown_Algorithm_Digits_Error"); } this.macMode = algorithm.toUpperCase().contains("HMAC"); this.messageDigest = this.macMode ? null : this.initDigest(algorithm); this.hmac = this.macMode ? this.initHmac(algorithm, keyBytes) : null; } /** *

Abstract method for initialize MessageDigest instance

*

抽象方法用于初始化消息摘要算法适配器实例对象

* * @param algorithm Cipher Algorithm * 密码算法 * * @return Initialized MessageDigest instance * 初始化的消息摘要算法适配器 * * @throws CryptoException * If an error occurs when initialize MessageDigest * 当初始化消息摘要算法适配器实例对象时出现异常 */ protected abstract MessageDigest initDigest(final String algorithm) throws CryptoException; /** *

Abstract method for initialize Hmac instance

*

抽象方法用于初始化消息认证码适配器实例对象

* * @param algorithm Cipher Algorithm * 密码算法 * @param keyBytes Hmac key data bytes * 消息认证码算法密钥数据数组 * * @return Initialized Hmac instance * 初始化的消息认证码算法适配器 * * @throws CryptoException * If an error occurs when initialize Hmac instance * 当初始化消息认证码算法适配器实例对象时出现异常 */ protected abstract Mac initHmac(final String algorithm, final byte[] keyBytes) throws CryptoException; /** *

Append parts of given binary data array to current adapter

*

追加给定的二进制字节数组到当前适配器

* * @param dataBytes binary data array * 二进制字节数组 * @param position Data begin position * 数据起始坐标 * @param length Length of data append * 追加的数据长度 * * @throws CryptoException * If an error occurs when process data * 当处理数据时出现异常 */ @Override public final void append(final byte[] dataBytes, final int position, final int length) throws CryptoException { if (dataBytes.length < (position + length)) { throw new CryptoException(0x000000150001L, "Length_Not_Enough_Crypto_Error"); } if (this.macMode) { this.hmac.update(dataBytes, position, length); } else { this.messageDigest.update(dataBytes, position, length); } } /** *

Append parts of given binary data array to current adapter and calculate final result

*

追加给定的二进制字节数组到当前适配器并计算最终结果

* * @param dataBytes binary data array * 二进制字节数组 * @param position Data begin position * 数据起始坐标 * @param length Length of data append * 追加的数据长度 * * @return Calculate result data byte array * 计算的二进制字节数组结果 * * @throws CryptoException * If an error occurs when process data * 当处理数据时出现异常 */ @Override public final byte[] finish(final byte[] dataBytes, final int position, final int length) throws CryptoException { if (dataBytes.length < (position + length)) { throw new CryptoException(0x000000150001L, "Length_Not_Enough_Crypto_Error"); } this.append(dataBytes, position, length); byte[] result; if (this.macMode) { result = new byte[this.hmac.getMacSize()]; this.hmac.doFinal(result, 0); } else { result = this.messageDigest.digest(); } this.reset(); return result; } /** *

Verify given signature data bytes is valid

*

验证给定的签名二进制数据是合法的

* * @param signature signature data bytes * 签名二进制数据 * * @return Verify result * 验证结果 */ @Override public final boolean verify(final byte[] signature) { byte[] calcResult; if (this.macMode) { calcResult = new byte[this.hmac.getMacSize()]; this.hmac.doFinal(calcResult, 0); } else { calcResult = this.messageDigest.digest(); } boolean result = Arrays.equals(calcResult, signature); this.reset(); return result; } /** *

Reset current adapter

*

重置当前适配器

*/ @Override public final void reset() { if (this.macMode) { this.hmac.reset(); } else { this.messageDigest.reset(); } } /** *

Retrieve current mac size

*

读取当前消息认证码的长度

* * @return Mac size * 消息认证码的长度 */ public final int macLength() { return this.macMode ? this.hmac.getMacSize() : Globals.DEFAULT_VALUE_INT; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy