Question about opensll bignum functions


#1

I have a question about the following code, specifically the line using bignum.to_dec_str. What is it doing?

The reason I am asking is I need to recreate this function in C#. Step 1: convert string to sha256. I can do that. I don’t understand what the bignum functions are doing. I realize this may not be the right forum for this post–my apologizes if this belongs elsewhere.

extern crate openssl;
use self::openssl::sha::sha256;
use self::openssl::bn::BigNum;
use utils::error::BIG_NUMBER_ERROR;
pub fn encode(s: &str ) -> Result<String, u32> {
    match s.parse::<u32>() {
        Ok(_) => Ok(s.to_string()),
        Err(_) => {
            let hash = sha256(s.as_bytes());
            let bignum = match BigNum::from_slice(&hash) {
                Ok(b) => b,
                Err(_) => {
                    warn!("{}", BIG_NUMBER_ERROR.message);
                    return Err(BIG_NUMBER_ERROR.code_num)
                }
            };
            match bignum.to_dec_str() {
                Ok(s) => Ok(s.to_string()),
                Err(_) => {
                    warn!("{}", BIG_NUMBER_ERROR.message);
                    Err(BIG_NUMBER_ERROR.code_num)
                }
            }
        }
    }
}

Thnx
Matt


#2

https://docs.rs/openssl/0.10.15/openssl/bn/struct.BigNum.html#method.to_dec_str
View the source to see the ffi functions being used.


#3

I didn’t consider looking at the raw source. Something I forget is available in rust.