Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinBlandy committed Dec 9, 2023
1 parent 588e6dc commit f95e637
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 21 deletions.
60 changes: 39 additions & 21 deletions Codings/RSAHelper.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand All @@ -9,8 +10,12 @@
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Base64;
Expand All @@ -20,16 +25,15 @@
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;


public class RSAHelper {

public static void main(String[] args) throws Exception {

// 生成 RSA 密钥对
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048); // 密钥长度
keyPairGenerator.initialize(2048);
KeyPair keyPair = keyPairGenerator.generateKeyPair();

// 公钥和私钥
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
Expand All @@ -42,32 +46,30 @@ public static void main(String[] args) throws Exception {
ByteArrayOutputStream cipherOut = new ByteArrayOutputStream();
encode((Key) rsaPublicKey, new ByteArrayInputStream(raw.getBytes()), cipherOut);
System.out.println("公钥加密 - 密文:" + Base64.getEncoder().encodeToString(cipherOut.toByteArray()));

// 私钥解密
ByteArrayOutputStream rawOut = new ByteArrayOutputStream();
decode((Key) rsaPrivateKey, new ByteArrayInputStream(cipherOut.toByteArray()), rawOut);
System.out.println("私钥解密 - 原文:" + new String(rawOut.toByteArray()));


// 私钥加密
cipherOut = new ByteArrayOutputStream();
encode((Key) rsaPrivateKey, new ByteArrayInputStream(raw.getBytes()), cipherOut);
System.out.println("私钥加密 - 密文:" + Base64.getEncoder().encodeToString(cipherOut.toByteArray()));

// 公钥解密
rawOut = new ByteArrayOutputStream();
decode((Key) rsaPublicKey, new ByteArrayInputStream(cipherOut.toByteArray()), rawOut);
System.out.println("公钥解密 - 原文:" + new String(rawOut.toByteArray()));



// 签名 -------------------------------
// 计算签名
byte[] sign = sign(rsaPrivateKey, "SHA256withRSA", new ByteArrayInputStream(raw.getBytes()));
System.out.println("私钥签名:" + Base64.getEncoder().encodeToString(sign));

// 校验签名
boolean validate = validate(rsaPublicKey, "SHA256withRSA", new ByteArrayInputStream(raw.getBytes()), sign);
System.out.println("公钥校验:" +validate);
System.out.println("公钥校验:" + validate);
}

/**
Expand All @@ -80,11 +82,11 @@ public static void main(String[] args) throws Exception {
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws IOException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
public static void encode(Key key, InputStream in, OutputStream out)
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException, IllegalBlockSizeException, BadPaddingException {
public static void encode(Key key, InputStream in, OutputStream out) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException, IOException, IllegalBlockSizeException, BadPaddingException {
// 最大的加密明文长度
final int MAX_ENCRYPT_BLOCK = 245;

Expand All @@ -99,21 +101,22 @@ public static void encode(Key key, InputStream in, OutputStream out)
}
}


/**
* 解密
* @param key KEY
* @param in 输入参数
* @param out 输出解密后的原文
*
* @param key KEY
* @param in 输入参数
* @param out 输出解密后的原文
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws IOException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
public static void decode(Key key, InputStream in, OutputStream out) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException, IllegalBlockSizeException, BadPaddingException {

public static void decode(Key key, InputStream in, OutputStream out) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException, IOException, IllegalBlockSizeException, BadPaddingException {

// 最大的加密明文长度
final int MAX_DECRYPT_BLOCK = 256;

Expand Down Expand Up @@ -176,4 +179,19 @@ public static boolean validate(RSAPublicKey key, String algorithm, InputStream i
}
return signature.verify(sign);
}

/**
* 从证书解析出公钥
*
* @param pem
* @return
* @throws CertificateException
*/
public static PublicKey parsePublicKeyFromCertificate(InputStream pem) throws CertificateException {
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
// 获取证书链
// Collection<? extends Certificate> certificates = certFactory.generateCertificates(pem);
Certificate certificate = certFactory.generateCertificate(pem);
return certificate.getPublicKey();
}
}
3 changes: 3 additions & 0 deletions Go/lib/io/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,6 @@ func
func ReadFile(fsys FS, name string) ([]byte, error)
func ValidPath(name string) bool
func WalkDir(fsys FS, root string, fn WalkDirFunc) error
* 递归遍历 fsys 文件系统中的文件
* root 指定开始遍历的根文件/目录可以使用 "." 表示从根路径开始遍历
* fn 遍历到的文件目录回调
61 changes: 61 additions & 0 deletions Go/练习/监听文件内容的修改.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"bufio"
"io"
"log/slog"
"os"
"time"
)

func init() {

// log 日志记录器
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
AddSource: true,
Level: slog.LevelDebug,
})))
}

func main() {
file, err := os.Open("C:\\Users\\KevinBlandy\\Desktop\\adsense.txt")
if err != nil {
slog.Error("err", slog.String("err", err.Error()))
return
}

// 指针
var position int64 = 0

for {
_, err := file.Seek(position, io.SeekStart)
if err != nil {
slog.Error("seek err", slog.String("err", err.Error()))
return
}

scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)

for scanner.Scan() {
if err := scanner.Err(); err != nil {
slog.Error("scan err", slog.String("err", err.Error()))
return
}
line := scanner.Text()
slog.Debug(line)
}

// 获取到文件指针位置
count, err := file.Seek(0, io.SeekCurrent)
if err != nil {
slog.Error("seek err", slog.String("err", err.Error()))
return
}

// 更新指针
position = count

time.Sleep(time.Millisecond * 10)
}
}
34 changes: 34 additions & 0 deletions Java/java-密封类.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
------------------------
密封类
------------------------
# JDK 17 后新增的三个关键字
sealed:(封闭用于修饰类/接口代表这个类/接口为密封类/接口
non-sealed:(非封闭用于修饰类/接口代表这个类/接口为非密封类/接口
permits:(允许用于 extends implements 之后指定能够继承或实现封闭类的子类/接口


# 总结
1. sealed 声明类/接口是一个密封类
2. non-sealed 声明类/接口是一个非密封类

2. permits 如果是密封类那么设置允许继承实现当前类的类
* 如果当前类是 sealed 密封类直接子类也必须要声明当前类是 sealed 还是 non-sealed


public sealed interface Foo permits Bar, Zoo{}
// sealed 密封类
// permits Bar 和 Zoo 可以实现此接口

public non-sealed class Bar implements Foo {}
// non-sealed 非密封类

public sealed class Zoo implements Foo permits Qoo {}
// sealed 密封类
// permits Qoo 可以实现此接口

public non-sealed class Qoo extends Zoo {}
// non-sealed 非密封类

public class Koo extends Bar{}
// 继承 非密封类 Bar,不需要声明 sealed 或者是 non-sealed

15 changes: 15 additions & 0 deletions RabbitMQ/rabitmq-集群.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-----------------
普通集群
-----------------
# 队列分布在集群中的任意节点但是每个节点都同步 queue 的元数据
# 消费队列的时候如果连接到了非队列所在节点”,那么当前连接节点会从队列所在节点拉取数据
# 基本上就是说队列都完整放到某个节点上的节点宕机队列费不了了

-----------------
镜像集群
-----------------
# 高可用模式
# 队列会在多个节点上有复制节点”,每个节点都包含了完整的数据
# 写入消息的时候会把数据同步到其他节点


0 comments on commit f95e637

Please sign in to comment.