Skip to content

Commit

Permalink
write a line into test.file
Browse files Browse the repository at this point in the history
  • Loading branch information
huifer committed Jan 5, 2024
1 parent 29fabca commit 9c62eb4
Show file tree
Hide file tree
Showing 12 changed files with 1,451 additions and 0 deletions.
286 changes: 286 additions & 0 deletions docs/spring/csc9bd0806-ab6a-11ee-b0ee-acde48001122.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
package com.huifer.utils.utils;

import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.springframework.util.Assert;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.IOException;
import java.io.InputStream;
import java.security.*;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public final class RSAUtils {

/**
* 密钥算法
*/
private static final String KEY_ALGORITHM = "RSA";

/**
* 加密/解密算法
*/
private static final String TRANSFORMATION = "RSA/ECB/PKCS1Padding";

/**
* 安全服务提供者
*/
private static final Provider PROVIDER = new BouncyCastleProvider();

/**
* 不可实例化
*/
private RSAUtils() {
}

/**
* 生成密钥对
*
* @param keySize 密钥大小
* @return 密钥对
*/
public static KeyPair generateKeyPair(int keySize) {
Assert.state(keySize > 0);

try {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM, PROVIDER);
keyPairGenerator.initialize(keySize);
return keyPairGenerator.generateKeyPair();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e.getMessage(), e);
}
}

/**
* 生成私钥
*
* @param encodedKey 密钥编码
* @return 私钥
*/
public static PrivateKey generatePrivateKey(byte[] encodedKey) {
Assert.notNull(encodedKey);

try {
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM, PROVIDER);
return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(encodedKey));
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw new RuntimeException(e.getMessage(), e);
}
}

/**
* 生成私钥
*
* @param keyString 密钥字符串(BASE64编码)
* @return 私钥
*/
public static PrivateKey generatePrivateKey(String keyString) {
Assert.hasText(keyString);

return generatePrivateKey(Base64.decodeBase64(keyString));
}

/**
* 生成公钥
*
* @param encodedKey 密钥编码
* @return 公钥
*/
public static PublicKey generatePublicKey(byte[] encodedKey) {
Assert.notNull(encodedKey);

try {
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM, PROVIDER);
return keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw new RuntimeException(e.getMessage(), e);
}
}

/**
* 生成公钥
*
* @param keyString 密钥字符串(BASE64编码)
* @return 公钥
*/
public static PublicKey generatePublicKey(String keyString) {
Assert.hasText(keyString);

return generatePublicKey(Base64.decodeBase64(keyString));
}

/**
* 获取密钥字符串
*
* @param key 密钥
* @return 密钥字符串(BASE64编码)
*/
public static String getKeyString(Key key) {
Assert.notNull(key);

return Base64.encodeBase64String(key.getEncoded());
}

/**
* 获取密钥
*
* @param type 类型
* @param inputStream 输入流
* @param password 密码
* @return 密钥
*/
public static Key getKey(String type, InputStream inputStream, String password) {
Assert.hasText(type);
Assert.notNull(inputStream);

try {
KeyStore keyStore = KeyStore.getInstance(type, PROVIDER);
keyStore.load(inputStream, password != null ? password.toCharArray() : null);
String alias = keyStore.aliases().hasMoreElements() ? keyStore.aliases().nextElement() : null;
return keyStore.getKey(alias, password != null ? password.toCharArray() : null);
} catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException | UnrecoverableKeyException e) {
throw new RuntimeException(e.getMessage(), e);
}
}

/**
* 获取证书
*
* @param type 类型
* @param inputStream 输入流
* @return 证书
*/
public static Certificate getCertificate(String type, InputStream inputStream) {
Assert.hasText(type);
Assert.notNull(inputStream);

try {
CertificateFactory certificateFactory = CertificateFactory.getInstance(type, PROVIDER);
return certificateFactory.generateCertificate(inputStream);
} catch (CertificateException e) {
throw new RuntimeException(e.getMessage(), e);
}
}

/**
* 生成签名
*
* @param algorithm 签名算法
* @param privateKey 私钥
* @param data 数据
* @return 签名
*/
public static byte[] sign(String algorithm, PrivateKey privateKey, byte[] data) {
Assert.hasText(algorithm);
Assert.notNull(privateKey);
Assert.notNull(data);

try {
Signature signature = Signature.getInstance(algorithm, PROVIDER);
signature.initSign(privateKey);
signature.update(data);
return signature.sign();
} catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException e) {
throw new RuntimeException(e.getMessage(), e);
}
}

/**
* 验证签名
*
* @param algorithm 签名算法
* @param publicKey 公钥
* @param sign 签名
* @param data 数据
* @return 是否验证通过
*/
public static boolean verify(String algorithm, PublicKey publicKey, byte[] sign, byte[] data) {
Assert.hasText(algorithm);
Assert.notNull(publicKey);
Assert.notNull(sign);
Assert.notNull(data);

try {
Signature signature = Signature.getInstance(algorithm, PROVIDER);
signature.initVerify(publicKey);
signature.update(data);
return signature.verify(sign);
} catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException e) {
throw new RuntimeException(e.getMessage(), e);
}
}

/**
* 验证签名
*
* @param algorithm 签名算法
* @param certificate 证书
* @param sign 签名
* @param data 数据
* @return 是否验证通过
*/
public static boolean verify(String algorithm, Certificate certificate, byte[] sign, byte[] data) {
Assert.hasText(algorithm);
Assert.notNull(certificate);
Assert.notNull(sign);
Assert.notNull(data);

try {
Signature signature = Signature.getInstance(algorithm, PROVIDER);
signature.initVerify(certificate);
signature.update(data);
return signature.verify(sign);
} catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException e) {
throw new RuntimeException(e.getMessage(), e);
}
}

/**
* 加密
*
* @param publicKey 公钥
* @param data 数据
* @return 密文
*/
public static byte[] encrypt(PublicKey publicKey, byte[] data) {
Assert.notNull(publicKey);
Assert.notNull(data);

try {
Cipher cipher = Cipher.getInstance(TRANSFORMATION, PROVIDER);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(data);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
throw new RuntimeException(e.getMessage(), e);
}
}

/**
* 解密
*
* @param privateKey 私钥
* @param data 数据
* @return 明文
*/
public static byte[] decrypt(PrivateKey privateKey, byte[] data) {
Assert.notNull(privateKey);
Assert.notNull(data);

try {
Cipher cipher = Cipher.getInstance(TRANSFORMATION, PROVIDER);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(data);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
throw new RuntimeException(e.getMessage(), e);
}
}

}
73 changes: 73 additions & 0 deletions docs/spring/csca0d68be-ab6a-11ee-b0ee-acde48001122.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Copyright 2009-2019 the original author or authors.
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.apache.ibatis.submitted.ognlstatic;

import org.apache.ibatis.BaseDataTest;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.Reader;

class OgnlStaticTest {

private static SqlSessionFactory sqlSessionFactory;

@BeforeAll
static void setUp() throws Exception {
// create a SqlSessionFactory
try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/ognlstatic/mybatis-config.xml")) {
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}

// populate in-memory database
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
"org/apache/ibatis/submitted/ognlstatic/CreateDB.sql");
}

/**
* This is the log output.
* DEBUG [main] - ooo Using Connection [org.hsqldb.jdbc.JDBCConnection@5ae1a5c7]
* DEBUG [main] - ==> Preparing: SELECT * FROM users WHERE name IN (?) AND id = ?
* DEBUG [main] - ==> Parameters: 1(Integer), 1(Integer)
* There are two parameter mappings but DefaulParameterHandler maps them both to input paremeter (integer)
*/
@Test
// see issue #448
void shouldGetAUserStatic() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Mapper mapper = sqlSession.getMapper(Mapper.class);
User user = mapper.getUserStatic(1);
Assertions.assertNotNull(user);
Assertions.assertEquals("User1", user.getName());
}
}

@Test
// see issue #61 (gh)
void shouldGetAUserWithIfNode() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Mapper mapper = sqlSession.getMapper(Mapper.class);
User user = mapper.getUserIfNode("User1");
Assertions.assertEquals("User1", user.getName());
}
}

}
13 changes: 13 additions & 0 deletions docs/spring/csca4c1578-ab6a-11ee-b0ee-acde48001122.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.huifer.rbac.entity.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum TerminalEnums {
PC("PC"),
IOS("IOS"),
ANDROID("ANDROID");
private final String name;
}
Loading

0 comments on commit 9c62eb4

Please sign in to comment.