2

I'm doodling with this implementation of SHA-256. I'm trying to write a program that produces sha(0), sha(1), ... but I'm unable to. Naively I tried

#include <iostream>
#include "sha256.h"

int main(int argc, char *argv[]){ 
   for (int i=0; i < 4; i++)
      std::cout << sha256("i");
   return 0;
}

Of course, this doesn't produce sha256(0), sha256(1), ..., but rather interprets the i as the letter i, and not the integer variable i. Any advice on how to remedy this? Altering the function implentation itself is not feasible so I'm looking for another way. Clearly I don't know much C++ at all, but any advice would be much appreciated.

EDIT:

#include <iostream>
#include "sha256.h"
#include <sstream>

int main(int argc, char *argv[])
{
std::cout << "This is sha256("0"): \n" << sha256("0") << std::endl;
std::cout << "Loop: " << std::endl;
std::stringstream ss;
std::string result;
for (int i=0; i < 4; ++i)
{
    ss << i;
    ss >> result;
    std::cout << sha256(result) << std::endl;
}
return 0;
Erik Vesterlund
  • 481
  • 6
  • 19

1 Answers1

4

You need to transform the number i to the string i accepted by SHA. A straightforward option is to use the std::to_string C++11 function

std::cout << sha256(std::to_string(i)); 

In case you don't have access to a C++11 compiler (you should have, it's almost 2016), you can glance at this excellent link:

Easiest way to convert int to string in C++

Quick (not the most efficient) way of doing it with a std::stringstream:

#include <iostream>
#include <sstream>
#include "sha256.h"

int main()
{
    std::string result;
    std::stringstream ss;
    for (int i = 0; i < 4; i++)
    {
        ss << i;
        ss >> result;
        ss.clear(); // need to clear the eof flag so we can reuse it
        std::cout << sha256(result) << std::endl; 
    }
}
Community
  • 1
  • 1
vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • Thanks! I guess now all I have to do is fix the "to_string is not a member of std" glitch, hm... – Erik Vesterlund Nov 12 '15 at 03:55
  • @ErikVesterlund If you use gcc, make sure you compile with `-std=c++11` to enable C++11 support. If you don't have a C++11 compiler, then use a conversion method from the link I posted. – vsoftco Nov 12 '15 at 03:56
  • Well I'm writing the stuff in codeblocks, does it use gcc? Where should I write that? – Erik Vesterlund Nov 12 '15 at 03:59
  • Thanks. I've tried all that now and I just can't get it to work. to_string same problem (even restarted codeblocks after checking the relevant box in compiler settings) and itoa and stringstream don't work either. – Erik Vesterlund Nov 12 '15 at 04:15
  • `std::stringstream` or `itoa` should definitely work. Make sure you `#include ` for `std::stringstream` and `#include ` for `itoa`. Try avoiding `itoa` though since it is non-standard. Live example [here](http://coliru.stacked-crooked.com/a/3f056811087e8c02). – vsoftco Nov 12 '15 at 04:19
  • I got your example to work, finally. I just don't see how to use it? to_string was fairly obvious, its input was just my variable i. With stringstream I don't even understand what's going on (the documentation is of no help). – Erik Vesterlund Nov 12 '15 at 04:34
  • @ErikVesterlund just use the code I posted in your loop and replace the `x` with `i`. So define `std::stringstream ss; std::string result;` outside your loop, then the loop should look like `for(int i = 0; i < 4; ++i) { ss << i; ss >> result; std::cout << sha256(result);}`. – vsoftco Nov 12 '15 at 04:37
  • There's improvement, but it's not looping, just prints sha256(0) four times. I'm adding current code to OP. – Erik Vesterlund Nov 12 '15 at 04:48
  • @ErikVesterlund Ohh use `ss.clear();` after `ss >> result;` to clear the eof state of the stream. That should do it. Working example [here](http://coliru.stacked-crooked.com/a/12c0660e5b7e7e5c). – vsoftco Nov 12 '15 at 04:52
  • Now it's working! Thanks for doing all the work for me, but I really could not have solved any of these problems on my own :) – Erik Vesterlund Nov 12 '15 at 04:54