Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dep #46

Merged
merged 4 commits into from
Dec 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
move hex to dev
  • Loading branch information
kigawas committed Dec 5, 2020
commit a75d366707a1955c15b8916990afe66c27b89570
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ homepage = "https://ecies.org/rs/"
repository = "https://github.com/ecies/rs"

[dependencies]
hex = "0.4.2"
hkdf = "0.10.0"
libsecp256k1 = "0.3.5"
rand = "0.7.3"
Expand All @@ -33,6 +32,7 @@ pure = ["aes-gcm", "cipher"]
[dev-dependencies]
criterion = "0.3.3"
futures-util = "0.3.8"
hex = "0.4.2"
reqwest = "0.10.9"
tokio = "0.2.22"

Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ pub fn decrypt(receiver_sec: &[u8], msg: &[u8]) -> Result<Vec<u8>, SecpError> {

#[cfg(test)]
mod tests {
use super::*;
use hex::encode;
use utils::{decode_hex, generate_keypair};

use super::*;
use utils::generate_keypair;
use utils::tests::decode_hex;

const PYTHON_BACKEND: &str = "https://eciespy.herokuapp.com/";
const MSG: &str = "helloworld";
Expand Down
31 changes: 16 additions & 15 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use hex::decode;
use hkdf::Hkdf;
use rand::thread_rng;
use secp256k1::{util::FULL_PUBLIC_KEY_SIZE, PublicKey, SecretKey};
Expand All @@ -19,19 +18,6 @@ pub fn generate_keypair() -> (SecretKey, PublicKey) {
(sk.clone(), PublicKey::from_secret_key(&sk))
}

/// Remove 0x prefix of a hex string
pub fn remove0x(hex: &str) -> &str {
if hex.starts_with("0x") || hex.starts_with("0X") {
return &hex[2..];
}
hex
}

/// Convert hex string to u8 vector
pub fn decode_hex(hex: &str) -> Vec<u8> {
decode(remove0x(hex)).unwrap()
}

/// Calculate a shared AES key of our secret key and peer's public key by hkdf
pub fn encapsulate(sk: &SecretKey, peer_pk: &PublicKey) -> AesKey {
let mut shared_point = peer_pk.clone();
Expand Down Expand Up @@ -65,13 +51,28 @@ fn hkdf_sha256(master: &[u8]) -> AesKey {
}

#[cfg(test)]
mod tests {
pub(crate) mod tests {
use hex::decode;

use rand::{thread_rng, Rng};
use secp256k1::Error;

use super::*;
use crate::consts::{AES_IV_LENGTH, EMPTY_BYTES};

/// Remove 0x prefix of a hex string
pub fn remove0x(hex: &str) -> &str {
if hex.starts_with("0x") || hex.starts_with("0X") {
return &hex[2..];
}
hex
}

/// Convert hex string to u8 vector
pub fn decode_hex(hex: &str) -> Vec<u8> {
decode(remove0x(hex)).unwrap()
}

#[test]
fn test_remove_0x_decode_hex() {
assert_eq!(remove0x("0x0011"), "0011");
Expand Down