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

org.dromara.hutool.crypto.asymmetric.AsymmetricEncryptor 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.asymmetric;

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 java.io.InputStream;
import java.nio.charset.Charset;

/**
 * 非对称加密器接口,提供:
 * 
    *
  • 加密为bytes
  • *
  • 加密为Hex(16进制)
  • *
  • 加密为Base64
  • *
* * @author looly * @since 5.7.12 */ public interface AsymmetricEncryptor { /** * 加密 * * @param data 被加密的bytes * @param keyType 私钥或公钥 {@link KeyType} * @return 加密后的bytes */ byte[] encrypt(byte[] data, KeyType keyType); /** * 编码为Hex字符串 * * @param data 被加密的bytes * @param keyType 私钥或公钥 {@link KeyType} * @return Hex字符串 */ default String encryptHex(final byte[] data, final KeyType keyType) { return HexUtil.encodeStr(encrypt(data, keyType)); } /** * 编码为Base64字符串 * * @param data 被加密的bytes * @param keyType 私钥或公钥 {@link KeyType} * @return Base64字符串 * @since 4.0.1 */ default String encryptBase64(final byte[] data, final KeyType keyType) { return Base64.encode(encrypt(data, keyType)); } /** * 加密 * * @param data 被加密的字符串 * @param charset 编码 * @param keyType 私钥或公钥 {@link KeyType} * @return 加密后的bytes */ default byte[] encrypt(final String data, final Charset charset, final KeyType keyType) { return encrypt(ByteUtil.toBytes(data, charset), keyType); } /** * 加密,使用UTF-8编码 * * @param data 被加密的字符串 * @param keyType 私钥或公钥 {@link KeyType} * @return 加密后的bytes */ default byte[] encrypt(final String data, final KeyType keyType) { return encrypt(ByteUtil.toUtf8Bytes(data), keyType); } /** * 编码为Hex字符串 * * @param data 被加密的字符串 * @param keyType 私钥或公钥 {@link KeyType} * @return Hex字符串 * @since 4.0.1 */ default String encryptHex(final String data, final KeyType keyType) { return HexUtil.encodeStr(encrypt(data, keyType)); } /** * 编码为Hex字符串 * * @param data 被加密的bytes * @param charset 编码 * @param keyType 私钥或公钥 {@link KeyType} * @return Hex字符串 * @since 4.0.1 */ default String encryptHex(final String data, final Charset charset, final KeyType keyType) { return HexUtil.encodeStr(encrypt(data, charset, keyType)); } /** * 编码为Base64字符串,使用UTF-8编码 * * @param data 被加密的字符串 * @param keyType 私钥或公钥 {@link KeyType} * @return Base64字符串 * @since 4.0.1 */ default String encryptBase64(final String data, final KeyType keyType) { return Base64.encode(encrypt(data, keyType)); } /** * 编码为Base64字符串 * * @param data 被加密的字符串 * @param charset 编码 * @param keyType 私钥或公钥 {@link KeyType} * @return Base64字符串 * @since 4.0.1 */ default String encryptBase64(final String data, final Charset charset, final KeyType keyType) { return Base64.encode(encrypt(data, charset, keyType)); } /** * 加密 * * @param data 被加密的数据流 * @param keyType 私钥或公钥 {@link KeyType} * @return 加密后的bytes * @throws IORuntimeException IO异常 */ default byte[] encrypt(final InputStream data, final KeyType keyType) throws IORuntimeException { return encrypt(IoUtil.readBytes(data), keyType); } /** * 编码为Hex字符串 * * @param data 被加密的数据流 * @param keyType 私钥或公钥 {@link KeyType} * @return Hex字符串 * @since 4.0.1 */ default String encryptHex(final InputStream data, final KeyType keyType) { return HexUtil.encodeStr(encrypt(data, keyType)); } /** * 编码为Base64字符串 * * @param data 被加密的数据流 * @param keyType 私钥或公钥 {@link KeyType} * @return Base64字符串 * @since 4.0.1 */ default String encryptBase64(final InputStream data, final KeyType keyType) { return Base64.encode(encrypt(data, keyType)); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy