how to convert message digest to bigInteger in c++

For example say I have a message digest "cf91cc61b74a1feda9902ae9b9e96d9a" using md5()

How do I convert this message digest to a bigInteger so that I can use reduction function on it.



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
#include <iostream>
#include <string>
#include <boost/multiprecision/cpp_int.hpp>

int int_value( char hex_digit ) // invariant: valid hex digit
{
    if( hex_digit >= '0' && hex_digit <= '9' ) return hex_digit - '0' ;

    static const std::string af = "abcdefABCDEF" ;
    const auto p = af.find(hex_digit) ;
    return p%6 + 10 ;
}

boost::multiprecision::cpp_int to_big_int( const std::string& hex_str ) // invariant: valid hex string
{
    boost::multiprecision::cpp_int big_int = 0 ;

    for( char hex_digit : hex_str )
    {
        big_int *= 16 ;
        big_int += int_value(hex_digit) ;
    }

    return big_int ;
}

int main()
{
    const std::string hex_str = "cf91cc61b74a1feda9902ae9b9e96d9a" ;
    std::cout << to_big_int(hex_str) << '\n' ;
}
Boost Multiprecision understands hexadecimal strings without need for help:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <boost/multiprecision/cpp_int.hpp>
#include <iostream>
#include <string>

using integer = boost::multiprecision::cpp_int;

int main()
{
  const std::string s = "cf91cc61b74a1feda9902ae9b9e96d9a";
  integer n = integer( "0x" + s );
  
  std::cout << "s (hex) = " << s << "\n";
  std::cout << "n (dec) = " << std::dec << n << "\n";
  std::cout << "n (hex) = " << std::hex << n << "\n";
}


@JLBorges
I’m not convinced that testing the Arabic digits separately has much advantage compared to just putting them in the lookup string... but even so, I honestly don’t know of any hardware where A..F and a..f are not consecutively sequential.
Last edited on
Do i have to install the c++ boost library on my desktop to use it?

EDIT:
is alright. i copy and paste the file in the mingw directory.
Last edited on
Boost's multiprecision library does require being compiled to be fully functional.

There are several BigInt libraries that are header only.
https://duckduckgo.com/?q=big+integer+c%2B%2B+library+header+only&t=ffsb&ia=web
why not recode it so that the hash generates a big int instead of text?
@jonnin how do i make the md5() function generate big int?
you would have to rewrite it a little. Somewhere in there it cooks up the hex digits, and you place them into the big int instead of a string. It may not be worth it, but text to number and number to text are expensive if not necessary. If you need both a string and the value, it won't help. It only helps if you need the value, and not the string. If what you have is working, and fast enough, may also not be worth it.
Last edited on
@Duthomhas other than printing out the bigInteger, is it possible to mod/plus/minus the bigInteger variable?

for e.g. bigInteger n1 +/%- bigInteger n2?
Last edited on
You do not need to compile Boost Libraries to use the Boost cpp_int type.

A bignum integer behaves just like a machine integer at your level of abstraction, so yes, you can use all the usual arithmetic operations on it, and overloads of most of the math functions too.
Topic archived. No new replies allowed.