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

org.dromara.hutool.crypto.symmetric.SymmetricEncryptor Maven / Gradle / Ivy

There is a newer version: 6.0.0.M3
Show newest version
/*
 * Copyright (c) 2013-2024 Hutool Team and hutool.cn
 *
 * 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.dromara.hutool.crypto.symmetric;

import org.dromara.hutool.core.codec.binary.HexUtil;
import org.dromara.hutool.core.codec.binary.Base64;
import org.dromara.hutool.core.io.IORuntimeException;
import org.dromara.hutool.core.io.IoUtil;
import org.dromara.hutool.core.util.ByteUtil;
import org.dromara.hutool.core.util.CharsetUtil;

import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;

/**
 * 对称加密器接口,提供:
 * 
    *
  • 加密为bytes
  • *
  • 加密为Hex(16进制)
  • *
  • 加密为Base64
  • *
* * @author looly * @since 5.7.12 */ public interface SymmetricEncryptor { /** * 加密 * * @param data 被加密的bytes * @return 加密后的bytes */ byte[] encrypt(byte[] data); /** * 加密,针对大数据量,可选结束后是否关闭流 * * @param data 被加密的字符串 * @param out 输出流,可以是文件或网络位置 * @param isClose 是否关闭流 * @throws IORuntimeException IO异常 */ void encrypt(InputStream data, OutputStream out, boolean isClose); /** * 加密 * * @param data 数据 * @return 加密后的Hex */ default String encryptHex(final byte[] data) { return HexUtil.encodeStr(encrypt(data)); } /** * 加密 * * @param data 数据 * @return 加密后的Base64 */ default String encryptBase64(final byte[] data) { return Base64.encode(encrypt(data)); } /** * 加密 * * @param data 被加密的字符串 * @param charset 编码 * @return 加密后的bytes */ default byte[] encrypt(final String data, final Charset charset) { return encrypt(ByteUtil.toBytes(data, charset)); } /** * 加密 * * @param data 被加密的字符串 * @param charset 编码 * @return 加密后的Hex */ default String encryptHex(final String data, final Charset charset) { return HexUtil.encodeStr(encrypt(data, charset)); } /** * 加密 * * @param data 被加密的字符串 * @param charset 编码 * @return 加密后的Base64 * @since 4.5.12 */ default String encryptBase64(final String data, final Charset charset) { return Base64.encode(encrypt(data, charset)); } /** * 加密,使用UTF-8编码 * * @param data 被加密的字符串 * @return 加密后的bytes */ default byte[] encrypt(final String data) { return encrypt(ByteUtil.toBytes(data, CharsetUtil.UTF_8)); } /** * 加密,使用UTF-8编码 * * @param data 被加密的字符串 * @return 加密后的Hex */ default String encryptHex(final String data) { return HexUtil.encodeStr(encrypt(data)); } /** * 加密,使用UTF-8编码 * * @param data 被加密的字符串 * @return 加密后的Base64 */ default String encryptBase64(final String data) { return Base64.encode(encrypt(data)); } /** * 加密,加密后关闭流 * * @param data 被加密的字符串 * @return 加密后的bytes * @throws IORuntimeException IO异常 */ default byte[] encrypt(final InputStream data) throws IORuntimeException { return encrypt(IoUtil.readBytes(data)); } /** * 加密 * * @param data 被加密的字符串 * @return 加密后的Hex */ default String encryptHex(final InputStream data) { return HexUtil.encodeStr(encrypt(data)); } /** * 加密 * * @param data 被加密的字符串 * @return 加密后的Base64 */ default String encryptBase64(final InputStream data) { return Base64.encode(encrypt(data)); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy