Skip to content

Commit

Permalink
Replace openssl by crates 'num-bigint', 'sha2'
Browse files Browse the repository at this point in the history
Signed-off-by: Patrik Stas <patrik.stas@absa.africa>
  • Loading branch information
Patrik-Stas committed Jul 12, 2021
1 parent 3c2165d commit d51da45
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 28 deletions.
77 changes: 66 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion libvcx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ regex = "1.1.0"
rust-base58 = "0.0.4"
rmp-serde = "0.13.7"
base64 = "0.8.0"
openssl = { version = "0.10.35", features = ["vendored"] }
sha2 = "0.9.5"
num-bigint = "0.4.0"
num-traits = "0.2.0"
indy = "1.16.0-post-59"
indy-sys = "1.16.0-post-59"
Expand Down
1 change: 0 additions & 1 deletion libvcx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ extern crate lazy_static;
extern crate libc;
#[macro_use]
extern crate log;
extern crate openssl;
extern crate rand;
extern crate regex;
extern crate rmp_serde;
Expand Down
17 changes: 7 additions & 10 deletions libvcx/src/utils/openssl.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
use openssl::bn::BigNum;
use openssl::sha::sha256;
use sha2::{Sha256, Digest};
use num_bigint::BigUint;

use crate::error::prelude::*;

pub fn encode(s: &str) -> VcxResult<String> {
match s.parse::<u32>() {
Ok(val) => Ok(val.to_string()),
Err(_) => {
let hash = sha256(s.as_bytes());
let bignum = BigNum::from_slice(&hash)
.map_err(|err| VcxError::from_msg(VcxErrorKind::EncodeError, format!("Cannot encode string: {}", err)))?;

let encoded = bignum.to_dec_str()
.map_err(|err| VcxError::from_msg(VcxErrorKind::EncodeError, format!("Cannot encode string: {}", err)))?
.to_string();

let mut hasher = Sha256::new();
hasher.update(s.as_bytes());
let hash = hasher.finalize();
let bignum = BigUint::from_bytes_be(&hash.as_slice());
let encoded = bignum.to_str_radix(10);
Ok(encoded)
}
}
Expand Down
9 changes: 4 additions & 5 deletions libvcx/src/utils/validation.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
extern crate openssl;
extern crate rust_base58;

use crate::error::prelude::*;
use crate::settings::Actors;
use crate::utils::qualifier;

use self::openssl::bn::BigNum;
use self::rust_base58::FromBase58;
use num_bigint::BigUint;
use num_traits::Num;

pub fn validate_did(did: &str) -> VcxResult<String> {
if qualifier::is_fully_qualified(did) {
Expand All @@ -29,7 +29,6 @@ pub fn validate_did(did: &str) -> VcxResult<String> {
}

pub fn validate_verkey(verkey: &str) -> VcxResult<String> {
// assert len(base58.b58decode(ver_key)) == 32
let check_verkey = String::from(verkey);
match check_verkey.from_base58() {
Ok(ref x) if x.len() == 32 => Ok(check_verkey),
Expand All @@ -39,9 +38,9 @@ pub fn validate_verkey(verkey: &str) -> VcxResult<String> {
}

pub fn validate_nonce(nonce: &str) -> VcxResult<String> {
let nonce = BigNum::from_dec_str(nonce)
let nonce = BigUint::from_str_radix(nonce, 10)
.map_err(|err| VcxError::from_msg(VcxErrorKind::InvalidNonce, err))?;
if nonce.num_bits() > 80 {
if nonce.bits() > 80 {
return Err(VcxError::from_msg(VcxErrorKind::InvalidNonce, "Invalid Nonce length"));
}
Ok(nonce.to_string())
Expand Down

0 comments on commit d51da45

Please sign in to comment.