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串

1
hello world

加密

java

1
UgnRdVY7thSMN8dM5gg+wcMu7g8PmQclLFnoswkT9J8aB8RfU+LQhfKupRVifipg03LvVGedqQmXyF5Gz8AoDIW22q5kpLxpdS3sZdf2kQY6snDSHubtPszG+/jB05fYaCGCPvoJw1oD+HHWl6dF5GvTYvLcZykRAJtvr5vCjTg=

node

1
iQMK9U8wYsLdhssRVRVY0CA40SdM3PEP7KhuP74ICPA7AH0ckUw5Rq9C8RKAIBh0fO8hIYC+kPN9xUCKNUlFGy09xtBEOgRybg8JZZC8mEjeA0hHMbQePI4fxQa74IWYY646ihj7KCtaE55yE6G8yV7hRPFyjdPJieXEKhSJsmM=

js

1
hOv454LZNZhPoS2nbtnH7S1phhkdVVX0NtF7xx37H4tNKelTguQ0i/obZXBGVRC7FoP0CAP7lrqo/VlT276D9kLXHUGSl/9FW1Q4fNcj4iMZedjcQJaZNOSZ4Vlti/9cXUeuUXaGG6vS3V49EfMy/FELFy+zpbijcnrIAar6uh4=

解密

加密\解密 java node js
java 可以解密 可以解密 可以解密
node 可以解密 可以解密 可以解密
js 可以解密 可以解密 可以解密

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package pub.wii.cook.springboot.utils;

import lombok.SneakyThrows;

import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

public class RSAUtil {

private final static String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCOhQ81fJEKI/b0+iKtK0/WN7wcMJKuGL5QhzuRDyFLCADbKPkB7uGKfkQ1QDcntjZ8k4+mx2oGBKF5E1vSB+bD7SP7fkSgDmdCzGLSJX04Q9H95xuEfFsTXwW8dGh4uQDFt/dTIY2WAx8RnGhiJFKq6y9mj+tcofnzLbQNfy2PoQIDAQAB";
private final static String privateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAI6FDzV8kQoj9vT6Iq0rT9Y3vBwwkq4YvlCHO5EPIUsIANso+QHu4Yp+RDVANye2NnyTj6bHagYEoXkTW9IH5sPtI/t+RKAOZ0LMYtIlfThD0f3nG4R8WxNfBbx0aHi5AMW391MhjZYDHxGcaGIkUqrrL2aP61yh+fMttA1/LY+hAgMBAAECgYBuMZNA17+NB6G6aGzHV+WyzAU2Bphi497ChM0Zq4kial2/Fj7xr7HTUy2Jvszmd4xJZg579VOUs5/l7YHhMxrI2sUadaenkdJ/LmdHICRT4BAFdDnM78UsY1/YgtoA06r63ZkaE6PJ2UPMCgcYMqV5OkAFwt8oTDSGlBb4EZuUAQJBAMbR+tolF96tHPb96BKbowIytRQZpC3KMutnzY74davY4BvAnvzoUTs0d89Xi+1+YxmI0UahejGdyMEWOryNJYkCQQC3gf3B8Kmaic3hz1ghslsdY5n1sE8piJH/9owvToc54MVQc9sfFC9f+e6v8yjblHFeaoYTL6NUz9MUmeHAgatZAkB+VGnaNnuGR+UBo6/UMwROnz2jue8yESptnZVlZMYQHUu5FplvBYan4dzG6E/G5em+Dcs739qusB0hYyiLKfxRAkAiGKweqenJhgtUBqOYdzxIxKXpqZ272N1P0u6PJ6cmkOX4od438xcuXREFbkfMLNO3uFE7JWHSs17D+CejDjTZAkAWlJVrwvcgdPDun/xF2FK8YoonJFPJxFD/jrVPuoZ+HxYa9sytwuaAU8jTv8WDF4er1ZO4N3TENE+SedxuH0zW";

@SneakyThrows
public static PublicKey getPublicKey(String base64PublicKey) {
PublicKey publicKey;
X509EncodedKeySpec keySpec =
new X509EncodedKeySpec(Base64.getDecoder().decode(base64PublicKey.getBytes()));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
}

@SneakyThrows
public static PrivateKey getPrivateKey(String base64PrivateKey) {
PKCS8EncodedKeySpec keySpec =
new PKCS8EncodedKeySpec(Base64.getDecoder().decode(base64PrivateKey.getBytes()));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePrivate(keySpec);
}

@SneakyThrows
public static String encrypt(String data, String publicKey) {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, getPublicKey(publicKey));
return Base64.getEncoder().encodeToString(cipher.doFinal(data.getBytes()));
}

@SneakyThrows
public static String decrypt(byte[] data, PrivateKey privateKey) {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return new String(cipher.doFinal(data));
}

public static String decrypt(String data, String base64PrivateKey) {
return decrypt(Base64.getDecoder().decode(data.getBytes()), getPrivateKey(base64PrivateKey));
}

public static void main(String[] args) {
// 加密
String encryptedString = encrypt("hello world", publicKey);
System.out.println(encryptedString);
// 解密
String decryptedString = RSAUtil.decrypt(encryptedString, privateKey);
System.out.println(decryptedString);
}
}

Node

1
npm i node-rsa
1
2
3
4
5
6
7
8
9
10
11
12
const NodeRSA = require('node-rsa')
const privateKey = 'MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAI6FDzV8kQoj9vT6Iq0rT9Y3vBwwkq4YvlCHO5EPIUsIANso+QHu4Yp+RDVANye2NnyTj6bHagYEoXkTW9IH5sPtI/t+RKAOZ0LMYtIlfThD0f3nG4R8WxNfBbx0aHi5AMW391MhjZYDHxGcaGIkUqrrL2aP61yh+fMttA1/LY+hAgMBAAECgYBuMZNA17+NB6G6aGzHV+WyzAU2Bphi497ChM0Zq4kial2/Fj7xr7HTUy2Jvszmd4xJZg579VOUs5/l7YHhMxrI2sUadaenkdJ/LmdHICRT4BAFdDnM78UsY1/YgtoA06r63ZkaE6PJ2UPMCgcYMqV5OkAFwt8oTDSGlBb4EZuUAQJBAMbR+tolF96tHPb96BKbowIytRQZpC3KMutnzY74davY4BvAnvzoUTs0d89Xi+1+YxmI0UahejGdyMEWOryNJYkCQQC3gf3B8Kmaic3hz1ghslsdY5n1sE8piJH/9owvToc54MVQc9sfFC9f+e6v8yjblHFeaoYTL6NUz9MUmeHAgatZAkB+VGnaNnuGR+UBo6/UMwROnz2jue8yESptnZVlZMYQHUu5FplvBYan4dzG6E/G5em+Dcs739qusB0hYyiLKfxRAkAiGKweqenJhgtUBqOYdzxIxKXpqZ272N1P0u6PJ6cmkOX4od438xcuXREFbkfMLNO3uFE7JWHSs17D+CejDjTZAkAWlJVrwvcgdPDun/xF2FK8YoonJFPJxFD/jrVPuoZ+HxYa9sytwuaAU8jTv8WDF4er1ZO4N3TENE+SedxuH0zW'

var key = NodeRSA(privateKey, 'pkcs8-private-pem',
{'environment': 'node', 'encryptionScheme': 'pkcs1', 'signingScheme': 'sha256'});

// 加密
var encryptedData = key.encrypt('hello world', 'base64')
console.log(encryptedData)
// 解密
var decryptedData = key.decrypt(encryptedData, 'utf-8')
console.log(decryptedData)

JS

使用 openstack 实现的 jsencrypt.js

1
2
3
4
5
6
7
var encrypt=new JSEncrypt(); 
encrypt.setPrivateKey('MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAI6FDzV8kQoj9vT6Iq0rT9Y3vBwwkq4YvlCHO5EPIUsIANso+QHu4Yp+RDVANye2NnyTj6bHagYEoXkTW9IH5sPtI/t+RKAOZ0LMYtIlfThD0f3nG4R8WxNfBbx0aHi5AMW391MhjZYDHxGcaGIkUqrrL2aP61yh+fMttA1/LY+hAgMBAAECgYBuMZNA17+NB6G6aGzHV+WyzAU2Bphi497ChM0Zq4kial2/Fj7xr7HTUy2Jvszmd4xJZg579VOUs5/l7YHhMxrI2sUadaenkdJ/LmdHICRT4BAFdDnM78UsY1/YgtoA06r63ZkaE6PJ2UPMCgcYMqV5OkAFwt8oTDSGlBb4EZuUAQJBAMbR+tolF96tHPb96BKbowIytRQZpC3KMutnzY74davY4BvAnvzoUTs0d89Xi+1+YxmI0UahejGdyMEWOryNJYkCQQC3gf3B8Kmaic3hz1ghslsdY5n1sE8piJH/9owvToc54MVQc9sfFC9f+e6v8yjblHFeaoYTL6NUz9MUmeHAgatZAkB+VGnaNnuGR+UBo6/UMwROnz2jue8yESptnZVlZMYQHUu5FplvBYan4dzG6E/G5em+Dcs739qusB0hYyiLKfxRAkAiGKweqenJhgtUBqOYdzxIxKXpqZ272N1P0u6PJ6cmkOX4od438xcuXREFbkfMLNO3uFE7JWHSs17D+CejDjTZAkAWlJVrwvcgdPDun/xF2FK8YoonJFPJxFD/jrVPuoZ+HxYa9sytwuaAU8jTv8WDF4er1ZO4N3TENE+SedxuH0zW')

// 加密
var encryptedData = encrypt.encrypt('hello world')
// 解密
var decryptedData = encrypt.decrypt(encryptedData, 'utf-8')

参考

python logger

自定义 Logger

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
logger = logging.getLogger()
# 设置 level,不然可能不打印
logger.setLevel(logging.INFO)

# 打印格式
fmt = logging.Formatter('[%(asctime)s][%(filename)s][line:%(lineno)d][%(levelname)s] %(message)s',
'%Y-%m-%d %H:%M:%S')
# 打印到标准输
console_handler = logging.StreamHandler(stream=sys.stdout)
console_handler.setFormatter(fmt)
console_handler.setLevel(logging.INFO)
logger.addHandler(console_handler)
# 写入文件
file_handler = logging.handlers.RotatingFileHandler(file_path,
maxBytes=per_file_size,
backupCount=log_file_number)
file_handler.setFormatter(fmt)
file_handler.setLevel(logging.INFO)
logger.addHandler(file_handler)

企业的价值观

价值观

对于个人来说,价值观定义了一个人和这个世界的交互方式。对于企业呢,个人理解是定义了一个企业和它的员工、用户以及社会的交互方式。再来一个直接的问题,钱有价值吗?钱是货币,一种关于有劳动等价物对应的交换权的契约。钱本身没有价值,钱是一个人一段时间劳动的等价的表示。钱没有价值,钱所代表的劳动有价值。换句话说,价值不能脱离人存在。

团队

团队需要建设,团队建设需要智慧,不是讲讲道理就能提高凝聚力,平时对大家好一点,打仗的时候喊一句兄弟,才会显得不那么违和。

elastic data

修改配置

接口修改

1
2
3
4
5
6
7
8
9
10
11
12
# dev tools
PUT <index>
{
"settings" : {
"index" : {
"<name>" : <value>
}
}
}

# curl
curl -X PUT -u <user>:<password> "http://<host>:<port>/<index>" -d

索引配置

1
2
3
4
5
6
7
8
{
"settings" : {
"index" : {
"number_of_shards" : 5,
"number_of_replicas" : 5
}
}
}

boost - processing

Introduction

Boost Preprocessing Library 是 boost 定义的宏(macros)库,官方文档在这里

Data Types

Boost 的 Preprocess 库有四种数据类型,array、lists、sequences、tuples。

arrays

arrays 是包含两个元素的 tuple,第一个元素是 array 中元素的数量,第二个元素是包含 array 元素的 tuple。

定义

1
#define ARRAY (3, (a, b, c))

原语

lists

lists 是一个有头和尾的简单列表。头是一个元素,尾是另外一个 list 或者 BOOST_PP_NIL。

定义

1
#define LISTS (a, (b, (c, BOOST_PP_NIL)))

原语

sequences

sequences 是一组相邻的带括号的元素。

定义

1
#define SEQ (0)(1)(2)(3)

原语

tuples

元组是一个括号内用逗号分隔的元素列表。

定义

1
#define TUPLE (a, b, c)

原语

Reference

seq

BOOST_PP_SEQ_FOR_EACH

1
2
// 遍历 SEQ 中的每个元素,应用新的宏。
BOOST_PP_SEQ_FOR_EACH(macro, data, seq)
  • macro
    • 形如 macro(r, data, elem) 的宏。BOOST_PP_SEQ_FOR_EACH 遍历每个元素并使用该宏展开。
    • 元素
      • r:当前元素的索引(从 1 开始)
      • data:辅助数据
      • elem:当前元素
  • data: 辅助数据(可以不使用,使用 _ 作为占位符)
  • seq: 定义的序列

示例

1
2
3
4
5
6
7
8
9
10
11
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/seq/for_each.hpp>
#define SEQ (w)(x)(y)(z)
// cat
#define MACRO(r, data, elem) BOOST_PP_CAT(elem, data)
BOOST_PP_SEQ_FOR_EACH(MACRO, _, SEQ) // expands to w_ x_ y_ z_
// stringize
#define SIMPLE_SEQ (a)(b)(c)
#define MACRO(r, data, elem) BOOST_PP_STRINGIZE(r) BOOST_PP_STRINGIZE(data) BOOST_PP_STRINGIZE(elem)
std::string v = BOOST_PP_SEQ_FOR_EACH(MACRO, _, SIMPLE_SEQ); // v: 1_a2_b3_c
#undef MACRO

tuple

string

BOOST_PP_CAT

BOOST_PP_STRINGIZE