0

Ill start by saying I dont know anything about c++ and I googled, there is nothing that can make me understand how to use SHA1 in C++. found this one though Objective C: SHA1 but its about objective c im not sure it is the same.

I need to do the following:

I habve a lot of files, i compile them by doing ./make.sh and in one file called server.cpp I need to SHA1 info_hash thats inserted in this part:

Csql_query(m_database, "insert into @files (info_hash, mtime, ctime)
 values (?, unix_timestamp(), unix_timestamp())").p(i.first).execute();

so I kinda think I need to do the following sha1(?). "?" isnt info_hash i need. i think it generates somewhere and "?" is a variable.

so please dont tell me to add any classes or something else, because i do NOT understand how to do that, if i need to add something in the beggining of the file, please tell me so.

Community
  • 1
  • 1
Treat
  • 182
  • 1
  • 6
  • 15
  • 1
    Do I understand this right? You say that nothing will make you understand how to deal with this problem in C++. We are also not to tell you what to do because you wouldn't understand anyway. Why are you even asking a C++ related question? What kind of answer do you expect? C++ is not PHP, there is no inbuilt `sha1("cat")` function. – stakx - no longer contributing Aug 02 '16 at 19:43

3 Answers3

4

A good library for SHA1 and other functions is cryptopp.

Here is another question where the answer is cryptopp: Fast Cross-Platform C/C++ Hashing Library

mrtumnus
  • 347
  • 4
  • 17
Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
  • In PHP I do: sha1("cat") PHP returns 9d989e8d27dc9e0ec3389fc855f142c3d40f0c50 how can i do that in C++? – Treat Mar 04 '11 at 16:00
  • @Treat: See [How do I use a hash function?](http://www.cryptopp.com/fom-serve/cache/50.html) from the [Crypto++ FAQ](http://www.cryptopp.com/wiki/FAQ). – Greg Hewgill Mar 04 '11 at 20:07
  • To use SHA() you'll need libssl-dev and then link with -lssl -lcrypto and you can go further with (ARM8 /Graviton2 say) CPU support depending upon flags. Plenty of examples on GH nowadays given this question is 10 years old. A system call to bash would also be an option if performance does not matter. – mckenzm May 11 '21 at 06:38
1

It looks like the .p(i.first) part already substitutes the value of i.first into the ? in your query. Presumably i.first would be a value appropriate for the info_hash column.

Without seeing more of your code, it's impossible to say for sure.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • In PHP I do: sha1("cat") PHP returns 9d989e8d27dc9e0ec3389fc855f142c3d40f0c50 how can i do that in C++? – Treat Mar 04 '11 at 15:59
0

Here is a library that implements SHA1() - there are probably better i.e. more efficient libraries but this has a good explanation of how to use it.

If you want to calculate the SHA1 of a file there is a Microsoft utility for that.

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • Hi, @martin , it maybe does explain everything for people who understand atleast little bit from what c++ is about, but i have no idea what is library or how to use it(ok should be similar to php classes, but still) . – Treat Mar 04 '11 at 03:52
  • what i need is someone to edit the code i gave or to tell me how to edit it so the argument passed as info hash would be encoded by sha1 encoding, because i need it to be in table like sha1(word) because im doing the same with php and after i need to match them two together. – Treat Mar 04 '11 at 03:53
  • 3
    @Treat: You can't expect us to help if you're unwilling to put *any effort at all* into solving this yourself. – Greg Hewgill Mar 04 '11 at 10:47
  • @greg i do not want to be a c++ coder, i do not need to know that language at all. I just need to know how can i do this (i will show how it can be done in php) – Treat Mar 04 '11 at 15:56
  • I do: sha1("cat") PHP returns 9d989e8d27dc9e0ec3389fc855f142c3d40f0c50 how can i do that in C++? – Treat Mar 04 '11 at 15:58