将2个加解密方法移至crypto_util
This commit is contained in:
parent
851a6ecf5b
commit
4e5cfd4462
@ -11,11 +11,11 @@ use std::str::FromStr;
|
||||
|
||||
mod wallet;
|
||||
use secp256k1::PublicKey;
|
||||
use wallet::wallet_impl::{zdecrypt, zencrypt};
|
||||
use wallet_impl::Wallet;
|
||||
|
||||
use crate::wallet::*;
|
||||
mod utils;
|
||||
use utils::crypto_utils::{zdecrypt, zencrypt};
|
||||
use utils::str_utils::{base64_to_hex, hex_to_base64};
|
||||
|
||||
// #[cfg(target_os = "android")]
|
||||
|
@ -1,14 +1,16 @@
|
||||
use aes_gcm::{
|
||||
aead::{generic_array::GenericArray, Aead, KeyInit},
|
||||
Aes256Gcm,
|
||||
Nonce, // Or `Aes128Gcm`
|
||||
};
|
||||
use anyhow::Result;
|
||||
use argon2::{
|
||||
password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
|
||||
Argon2,
|
||||
};
|
||||
use base64::{engine::general_purpose, Engine as _};
|
||||
use ecies::{decrypt, encrypt};
|
||||
use rand::prelude::*;
|
||||
use secp256k1::{PublicKey, SecretKey};
|
||||
use std::str;
|
||||
use tiny_keccak::keccak256;
|
||||
|
||||
@ -119,3 +121,42 @@ pub fn aes_decrypt(str: &str, key: &str) -> String {
|
||||
let plaintext = str::from_utf8(&plaintext).expect("err convert to utf8");
|
||||
plaintext.to_string()
|
||||
}
|
||||
|
||||
pub fn zencrypt(pk: PublicKey, msg: &str) -> Result<String> {
|
||||
let pk = &pk.serialize();
|
||||
let msg = msg.as_bytes();
|
||||
// println!("msg before encrypt: {:?}", msg);
|
||||
let msg_encrypt = match encrypt(pk, &msg) {
|
||||
Ok(v) => v,
|
||||
Err(e) => panic!("error encrypt content: {}", e),
|
||||
};
|
||||
// println!("msg after encrypt: {:?}", &msg_encrypt);
|
||||
let str_encrypt = hex::encode(&msg_encrypt);
|
||||
Ok(str_encrypt)
|
||||
}
|
||||
|
||||
pub fn zdecrypt(sk: SecretKey, msg1: &str) -> Result<String> {
|
||||
let sk = sk.secret_bytes();
|
||||
let mut msg: String = msg1.clone().to_string();
|
||||
if msg.len() % 2 == 1 {
|
||||
msg = "0".to_owned() + &msg;
|
||||
}
|
||||
println!("msg to decrypt: {:?}", &msg);
|
||||
let msg = match hex::decode(&msg) {
|
||||
Ok(v) => v,
|
||||
Err(e) => panic!("error decode hex str: {}", e),
|
||||
};
|
||||
let msg_decrypt = match decrypt(&sk, &msg) {
|
||||
Ok(v) => v,
|
||||
Err(e) => panic!("error decrypt content: {}", e),
|
||||
};
|
||||
// println!("msg after decrypt: {:?}", &msg_decrypt);
|
||||
// let msg_decrypt = hex::encode(msg_decrypt);
|
||||
let str_decrypt = match str::from_utf8(&msg_decrypt) {
|
||||
Ok(v) => v,
|
||||
Err(e) => panic!("Invalid UTF-8 sequence: {}", e),
|
||||
};
|
||||
let result = str_decrypt.to_string();
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ extern crate hex;
|
||||
|
||||
use anyhow::Result;
|
||||
use core::fmt::Write;
|
||||
use ecies::{decrypt, encrypt};
|
||||
use primitive_types::{H160, H256};
|
||||
use secp256k1::rand::rngs::OsRng;
|
||||
use secp256k1::{Message, PublicKey, Secp256k1, SecretKey};
|
||||
@ -19,45 +18,6 @@ pub fn generate_keypair() -> (SecretKey, PublicKey) {
|
||||
secp.generate_keypair(&mut OsRng)
|
||||
}
|
||||
|
||||
pub fn zencrypt(pk: PublicKey, msg: &str) -> Result<String> {
|
||||
let pk = &pk.serialize();
|
||||
let msg = msg.as_bytes();
|
||||
// println!("msg before encrypt: {:?}", msg);
|
||||
let msg_encrypt = match encrypt(pk, &msg) {
|
||||
Ok(v) => v,
|
||||
Err(e) => panic!("error encrypt content: {}", e),
|
||||
};
|
||||
// println!("msg after encrypt: {:?}", &msg_encrypt);
|
||||
let str_encrypt = hex::encode(&msg_encrypt);
|
||||
Ok(str_encrypt)
|
||||
}
|
||||
|
||||
pub fn zdecrypt(sk: SecretKey, msg1: &str) -> Result<String> {
|
||||
let sk = sk.secret_bytes();
|
||||
let mut msg: String = msg1.clone().to_string();
|
||||
if msg.len() % 2 == 1 {
|
||||
msg = "0".to_owned() + &msg;
|
||||
}
|
||||
println!("msg to decrypt: {:?}", &msg);
|
||||
let msg = match hex::decode(&msg) {
|
||||
Ok(v) => v,
|
||||
Err(e) => panic!("error decode hex str: {}", e),
|
||||
};
|
||||
let msg_decrypt = match decrypt(&sk, &msg) {
|
||||
Ok(v) => v,
|
||||
Err(e) => panic!("error decrypt content: {}", e),
|
||||
};
|
||||
// println!("msg after decrypt: {:?}", &msg_decrypt);
|
||||
// let msg_decrypt = hex::encode(msg_decrypt);
|
||||
let str_decrypt = match str::from_utf8(&msg_decrypt) {
|
||||
Ok(v) => v,
|
||||
Err(e) => panic!("Invalid UTF-8 sequence: {}", e),
|
||||
};
|
||||
let result = str_decrypt.to_string();
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn public_key_address(public_key: &PublicKey) -> H160 {
|
||||
let public_key = public_key.serialize_uncompressed();
|
||||
debug_assert_eq!(public_key[0], 0x04);
|
||||
|
Loading…
x
Reference in New Issue
Block a user