forked from alibaba/nacos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EncryptionHandler.java
110 lines (100 loc) · 4 KB
/
EncryptionHandler.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* 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 com.alibaba.nacos.plugin.encryption.handler;
import com.alibaba.nacos.common.utils.Pair;
import com.alibaba.nacos.plugin.encryption.EncryptionPluginManager;
import com.alibaba.nacos.plugin.encryption.spi.EncryptionPluginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Optional;
import java.util.stream.Stream;
/**
* EncryptionHandler.
*
* @author lixiaoshuang
*/
public class EncryptionHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(EncryptionHandler.class);
/**
* For example:cipher-AES-dataId.
*/
private static final String PREFIX = "cipher-";
/**
* Execute encryption.
*
* @param dataId dataId
* @param content Content that needs to be encrypted.
* @return Return key and ciphertext.
*/
public static Pair<String, String> encryptHandler(String dataId, String content) {
if (!checkCipher(dataId)) {
return Pair.with("", content);
}
Optional<String> algorithmName = parseAlgorithmName(dataId);
Optional<EncryptionPluginService> optional = algorithmName
.flatMap(EncryptionPluginManager.instance()::findEncryptionService);
if (!optional.isPresent()) {
LOGGER.warn("[EncryptionHandler] [encryptHandler] No encryption program with the corresponding name found");
return Pair.with("", content);
}
EncryptionPluginService encryptionPluginService = optional.get();
String secretKey = encryptionPluginService.generateSecretKey();
String encryptContent = encryptionPluginService.encrypt(secretKey, content);
return Pair.with(encryptionPluginService.encryptSecretKey(secretKey), encryptContent);
}
/**
* Execute decryption.
*
* @param dataId dataId
* @param secretKey Decryption key.
* @param content Content that needs to be decrypted.
* @return Return key and plaintext.
*/
public static Pair<String, String> decryptHandler(String dataId, String secretKey, String content) {
if (!checkCipher(dataId)) {
return Pair.with("", content);
}
Optional<String> algorithmName = parseAlgorithmName(dataId);
Optional<EncryptionPluginService> optional = algorithmName
.flatMap(EncryptionPluginManager.instance()::findEncryptionService);
if (!optional.isPresent()) {
LOGGER.warn("[EncryptionHandler] [decryptHandler] No encryption program with the corresponding name found");
return Pair.with("", content);
}
EncryptionPluginService encryptionPluginService = optional.get();
String decryptSecretKey = encryptionPluginService.decryptSecretKey(secretKey);
String decryptContent = encryptionPluginService.decrypt(decryptSecretKey, content);
return Pair.with(decryptSecretKey, decryptContent);
}
/**
* Parse encryption algorithm name.
*
* @param dataId dataId
* @return algorithm name
*/
private static Optional<String> parseAlgorithmName(String dataId) {
return Stream.of(dataId.split("-")).skip(1).findAny();
}
/**
* Check if encryption and decryption is needed.
*
* @param dataId dataId
* @return boolean
*/
private static boolean checkCipher(String dataId) {
return dataId.startsWith(PREFIX) && !dataId.equals(PREFIX);
}
}