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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
use ;
use Data;
use HashMap;
use crate;
pub async
pub async
/*#[utoipa::path(
post,
path = "/anttp-0/crypto/encrypt/{public_key}",
params(
("public_key" = String, Path, description = "Public key as hex string"),
),
request_body(
content = HashMap<String, CryptoContent>,
description = "Map of base64 data to CryptoContent struct",
example = json!({
"aGVsbG8gd29ybGQ=": {
}
})
),
responses(
(status = OK, description = "Encryption results", body = HashMap<String, CryptoContent>),
)
)]
pub async fn post_encrypt(
path: web::Path<String>,
crypto_service: Data<CryptoService>,
data_map: web::Json<HashMap<String, CryptoContent>>,
) -> HttpResponse {
let public_key = path.into_inner();
let result = crypto_service.encrypt_map(public_key, data_map.into_inner());
HttpResponse::Ok().json(result)
}*/
/*#[utoipa::path(
post,
path = "/anttp-0/crypto/decrypt",
request_body(
content = HashMap<String, CryptoContent>,
description = "Map of base64 encrypted data to CryptoContent struct",
example = json!({
"aGVsbG8gd29ybGQ=": {
}
})
),
responses(
(status = OK, description = "Decryption results", body = HashMap<String, CryptoContent>),
)
)]
pub async fn post_decrypt(
crypto_service: Data<CryptoService>,
data_map: web::Json<HashMap<String, CryptoContent>>,
) -> HttpResponse {
let result = crypto_service.decrypt_map(data_map.into_inner());
HttpResponse::Ok().json(result)
}*/
/*#[cfg(test)]
mod tests {
use super::*;
use actix_web::{test, App};
use blsttc::SecretKey;
use base64::{engine::general_purpose, Engine as _};
use crate::config::anttp_config::AntTpConfig;
use clap::Parser;
#[actix_web::test]
async fn test_post_encrypt_success() {
let secret_key = SecretKey::random();
let public_key_hex = hex::encode(secret_key.public_key().to_bytes());
let data = b"hello world";
let data_base64 = general_purpose::STANDARD.encode(data);
let ant_tp_config = AntTpConfig::parse_from(&["anttp"]);
let crypto_service = Data::new(CryptoService::new(ant_tp_config));
let app = test::init_service(
App::new()
.app_data(crypto_service.clone())
.route("/anttp-0/crypto/encrypt/{public_key}", web::post().to(post_encrypt))
).await;
let mut data_map = HashMap::new();
data_map.insert(data_base64.clone(), CryptoContent {
content: None,
});
let req = test::TestRequest::post()
.uri(&format!("/anttp-0/crypto/encrypt/{}", public_key_hex))
.set_json(&data_map)
.to_request();
let resp: HashMap<String, CryptoContent> = test::call_and_read_body_json(&app, req).await;
assert!(resp.contains_key(&data_base64));
let crypto_content_struct = resp.get(&data_base64).unwrap();
assert!(crypto_content_struct.content.is_some());
let encrypted_base64 = crypto_content_struct.content.as_ref().unwrap();
let encrypted_bytes = general_purpose::STANDARD.decode(encrypted_base64).unwrap();
let ciphertext = blsttc::Ciphertext::from_bytes(&encrypted_bytes).unwrap();
let decrypted_data = secret_key.decrypt(&ciphertext).unwrap();
assert_eq!(decrypted_data, data);
}
#[actix_web::test]
async fn test_post_decrypt_success() {
let secret_key = SecretKey::random();
let app_private_key_hex = secret_key.to_hex();
let data = b"hello world";
let encrypted_data = secret_key.public_key().encrypt(data).to_bytes();
let encrypted_data_base64 = general_purpose::STANDARD.encode(encrypted_data);
let ant_tp_config = AntTpConfig::parse_from(&["anttp", "--app-private-key", &app_private_key_hex]);
let crypto_service = Data::new(CryptoService::new(ant_tp_config));
let app = test::init_service(
App::new()
.app_data(crypto_service.clone())
.route("/anttp-0/crypto/decrypt", web::post().to(post_decrypt))
).await;
let mut data_map = HashMap::new();
data_map.insert(encrypted_data_base64.clone(), CryptoContent {
content: None,
});
let req = test::TestRequest::post()
.uri("/anttp-0/crypto/decrypt")
.set_json(&data_map)
.to_request();
let resp: HashMap<String, CryptoContent> = test::call_and_read_body_json(&app, req).await;
assert!(resp.contains_key(&encrypted_data_base64));
let crypto_content_struct = resp.get(&encrypted_data_base64).unwrap();
assert!(crypto_content_struct.content.is_some());
let decrypted_base64 = crypto_content_struct.content.as_ref().unwrap();
let decrypted_bytes = general_purpose::STANDARD.decode(decrypted_base64).unwrap();
assert_eq!(decrypted_bytes, data);
}
#[actix_web::test]
async fn test_post_sign_success() {
let secret_key = SecretKey::random();
let app_private_key_hex = secret_key.to_hex();
let data_hex = hex::encode(b"hello world");
let ant_tp_config = AntTpConfig::parse_from(&["anttp", "--app-private-key", &app_private_key_hex]);
let crypto_service = Data::new(CryptoService::new(ant_tp_config));
let app = test::init_service(
App::new()
.app_data(crypto_service.clone())
.route("/anttp-0/crypto/sign", web::post().to(post_sign))
).await;
let mut data_map = HashMap::new();
data_map.insert(data_hex.clone(), Crypto {
signature: None,
verified: None,
});
let req = test::TestRequest::post()
.uri("/anttp-0/crypto/sign")
.set_json(&data_map)
.to_request();
let resp: HashMap<String, Crypto> = test::call_and_read_body_json(&app, req).await;
assert!(resp.contains_key(&data_hex));
let crypto_struct = resp.get(&data_hex).unwrap();
assert!(crypto_struct.verified.unwrap());
assert!(crypto_struct.signature.is_some());
}
#[actix_web::test]
async fn test_post_verify_success() {
let secret_key = SecretKey::random();
let public_key = hex::encode(secret_key.public_key().to_bytes());
let data = b"hello world";
let data_hex = hex::encode(data);
let signature = hex::encode(secret_key.sign(data).to_bytes());
let ant_tp_config = AntTpConfig::parse_from(&["anttp"]);
let crypto_service = Data::new(CryptoService::new(ant_tp_config));
let app = test::init_service(
App::new()
.app_data(crypto_service.clone())
.route("/anttp-0/crypto/verify/{public_key}", web::post().to(post_verify))
).await;
let mut data_map = HashMap::new();
data_map.insert(data_hex.clone(), Crypto {
signature: Some(signature),
verified: None,
});
let req = test::TestRequest::post()
.uri(&format!("/anttp-0/crypto/verify/{}", public_key))
.set_json(&data_map)
.to_request();
let resp: HashMap<String, Crypto> = test::call_and_read_body_json(&app, req).await;
assert!(resp.contains_key(&data_hex));
assert!(resp.get(&data_hex).unwrap().verified.unwrap());
}
}
*/