On Thu, Apr 27, 2023 at 12:19 AM Dwight Kulkarni <dwi...@realtime-7.com> wrote:
>
> Your code is exactly what I had before. But I was getting that heap error. It 
> only went away when I used NEW. I have never seen that before. I also don't 
> like using NEW at all but it wasn't working.

convert_cryptopp_intege and convert_cryptopp_integer_str test fine for
the attached program. You have a problem somewhere else in your
program.

$ g++ -o test.exe -g3 -O1 -fsanitize=address test.cxx ./libcryptopp.a
$ ./test.exe
Iostream: e9e930805f30e3ac95845917bef1d708h
  Vector: e9e930805f30e3ac95845917bef1d708
Convert1: e9e930805f30e3ac95845917bef1d708
Convert2: e9e930805f30e3ac95845917bef1d708

Jeff

-- 
You received this message because you are subscribed to the Google Groups 
"Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to cryptopp-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/cryptopp-users/CAH8yC8%3D_bzpPrTJZTmMGqjGF1deScu-rvuO%2BX4%2Bw3ne2pVC0zA%40mail.gmail.com.
#include <iostream>
#include <iomanip>
#include "integer.h"
#include "osrng.h"

std::string convert_cryptopp_integer_str(const CryptoPP::Integer& n)
{
    using namespace CryptoPP;

    const size_t len = n.MinEncodedSize(Integer::UNSIGNED);
    std::string v; v.resize(len);

    n.Encode((byte*)&v[0], v.size(), Integer::UNSIGNED);
    return v;
    
}

std::vector<CryptoPP::byte> convert_cryptopp_integer(const CryptoPP::Integer& n)
{
    using namespace CryptoPP;

    const size_t len = n.MinEncodedSize(Integer::UNSIGNED);
    std::vector<byte> v(len);

    n.Encode((byte*)&v[0], v.size(), Integer::UNSIGNED);
    return v; 
}

int main(int argc, char* argv[])
{
    using namespace CryptoPP;
    
    AutoSeededRandomPool prng;
    Integer n;
    
    n.Randomize(prng, 128);
    const size_t len = n.MinEncodedSize(Integer::UNSIGNED);

    // Error in vector size
    std::vector<byte> v(len);
    n.Encode((byte*)&v[0], v.size(), Integer::UNSIGNED);

    std::cout << "Iostream: " << std::hex << n << std::endl;
    std::cout << "  Vector: ";
    for(byte i : v) {
      std::cout << std::hex << std::setw(2) << std::setfill('0') << (i & 0xff);
    }
    std::cout << std::endl;

    std::cout << "Convert1: ";
    std::string w = convert_cryptopp_integer_str(n);
    for(byte i : w) {
      std::cout << std::hex << std::setw(2) << std::setfill('0') << (i & 0xff);
    }
    std::cout << std::endl;

    std::cout << "Convert2: ";
    std::vector<byte> x = convert_cryptopp_integer(n);
    for(byte i : x) {
      std::cout << std::hex << std::setw(2) << std::setfill('0') << (i & 0xff);
    }
    std::cout << std::endl;

    return 0;
}

Reply via email to