-1

Before i start i would like to say that i am a complete beginner when it comes to c++, but i am familiar with other programming languages.

I am attempting to write a code that will take a string, and hash it X number of times.

Pseudo code:

Times to hash = X
String to hash = "Hello world"
For i=1 to 10
   hash string
next
Console write string that has been hashed X times.

I have done some research and i have modified this code, but the problem is the hash algorithm is md5 and i would like it to be sha256 (but i couldn't find an useful code i could adapt)

#include "md5.h"
#include <string>
#include <iostream>
using namespace std;
int main()
{
  MD5 md5 ;
  string str1(md5.digestString( "String" ));
  cout << str1;
  return 0;
}

I have had look a several for loops but i can seem to get any of them to work but here is on of my attempts:

int i = 0;
for (; i<50; ++i) {
    cout << "Hello world";
}

The error for the for loop:

2   1   C:\Users\09stephenb\Desktop\Hash.cpp    [Error] expected unqualified-id before 'for'
2   8   C:\Users\09stephenb\Desktop\Hash.cpp    [Error] 'i' does not name a type
2   14  C:\Users\09stephenb\Desktop\Hash.cpp    [Error] expected unqualified-id before '++' token

But im getting errors like "i is not a type", im assuming variable type, but im not quite sure what to do next.

I am somewhat reluctant to learn C++ fully at this time as this is just a tiny section of the project that i am working on.

Edit

Form the comments and answers i have this code:

#include <iostream>
#include "md5.h"
#include <string>
using namespace std;

int main() {
    int count = 50;
    std::string prev = "stringtohash";
    std::string cur;
    
    for(int i=0; i < count; ++i) {
        cur = md5.digestString(prev);
        prev = cur;
    }
    
    std::cout << cur;
    return 0;
}

But it returns the error: [Error] 'md5' was not declared in this scope

Any help would be greatly appreciated.

Community
  • 1
  • 1
09stephenb
  • 9,358
  • 15
  • 53
  • 91
  • `cout "Hello world"` this is not valid. – Borgleader Sep 14 '15 at 18:12
  • @Borgleader I missed out the `<<`, edited. – 09stephenb Sep 14 '15 at 18:14
  • Missed `;` at the end of `cout << "Hello"` .... – PiotrNycz Sep 14 '15 at 18:16
  • @PiotrNycz, Edited, like i said im new to C++ – 09stephenb Sep 14 '15 at 18:18
  • Could you please copy paste the actual code, and error message? – MikeMB Sep 14 '15 at 18:20
  • Try to search in internet the following "sha256 freeware C". Then try to use one of what you found for single string, Then try to do it X times in loop. Then if you have problems - ask for help... – PiotrNycz Sep 14 '15 at 18:23
  • 1
    The error message doesn't fit the code at all (the code seems ok). Can you create an mcve? – MikeMB Sep 14 '15 at 18:27
  • Please remove the C tag - This is C++. Then, check your pseudo-code: you have `Times to hash = X` and then `For i=1 to 10` but I think you want `For i=1 to X`. Finally, please provide a [minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) where we can reproduce your error. – Fabio says Reinstate Monica Sep 14 '15 at 18:35
  • @FabioTurati When i run the for loop in my question i revive the error: "[Error] 'i' does not name a type", can you reproduce the error? – 09stephenb Sep 14 '15 at 18:39
  • Your response to @longwei, ` 'string' in namespace 'std' does not name a type` makers no sense when including string. Remove the include of md5.h and references to it so you just have a glorified "hello world" with a loop. This should allow you to see if there is a mistake like a missing `;` in md5.h. Won't find it, but you've narrowed down the search area. – user4581301 Sep 14 '15 at 18:48
  • 1
    new error is because you've removed the definition of md5 you had in the previous version. `MD5 md5;`. If putting that definition back in breaks other stuff, seriously examine `MD5`. – user4581301 Sep 14 '15 at 19:07
  • Moving iostream's include above md5's helping says md5 is missing at least one required include. Best to keep headers self-consistent by having the header include everything it needs. – user4581301 Sep 14 '15 at 19:10
  • Oh, and consider removing that `using namespace std;` For why, read: http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – user4581301 Sep 14 '15 at 19:13
  • @user4581301 After adding MD5 md5; back into the code i revive the following error `[Error] no matching function for call to 'MD5::digestString(std::string&)'`. – 09stephenb Sep 14 '15 at 19:18
  • Recommend adding md5.h to your question unless it's a huge file. – user4581301 Sep 14 '15 at 19:24
  • @user4581301 A link to it is in the question, i didn't add the code to the question as it is over 100 lines long. – 09stephenb Sep 14 '15 at 19:27
  • `std::string prev` is not the same as `char *string`. Normal trick is to call `prev.c_str()`, but that returns `const char *` (I am constant., Do not try to change me!), so you can't use that either. As loath as I am to say this, you're going to have to stop using std::string or do stupid copies from std::string to char arrays or modify the MD5 class. – user4581301 Sep 14 '15 at 19:35

2 Answers2

0

The loop may be this:

for (int i = 0; i<50; i++) {
     cout << "Hello world"
}

For sha256 check http://www.cryptlib.com/

Added the "<<" operator as Fabio Turati said.

Johna
  • 2,623
  • 3
  • 21
  • 36
0

almost same as your pseudo code :)

int count = 50;
std::string cur = "stringtohash";

for(int i=0; i < count; ++i) {
    cur = whateverlib.md5.digest(cur );
}

std::cout << cur;
Longwei
  • 66
  • 3
  • The code looks good but i am reciveing the following errors: `2 6 C:\Users\09stephenb\Desktop\Hash.cpp [Error] 'string' in namespace 'std' does not name a type 3 6 C:\Users\09stephenb\Desktop\Hash.cpp [Error] 'string' in namespace 'std' does not name a type 5 1 C:\Users\09stephenb\Desktop\Hash.cpp [Error] expected unqualified-id before 'for' 5 14 C:\Users\09stephenb\Desktop\Hash.cpp [Error] 'i' does not name a type 5 25 C:\Users\09stephenb\Desktop\Hash.cpp [Error] expected unqualified-id before '++' token` – 09stephenb Sep 14 '15 at 18:33
  • Std string doesn't copy on write anymore, as it's previous implementation is illegal in c++11. Aside from that I dont understand the purpose of prev – MikeMB Sep 14 '15 at 18:42
  • After help form @Johna I have mannaged to get the code to work with just 1 error `[Error] 'whateverlib' was not declared in this scope`. – 09stephenb Sep 14 '15 at 18:57