c++哈希字符串X次,然后写入控制台
C++ hash string X number of times and then write to console
在我开始之前,我想说的是,当谈到c++时,我是一个完全的初学者,但我熟悉其他编程语言。
我正在尝试写一个代码,将一个字符串,并哈希它X次。
伪代码:
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.
我做了一些研究,我修改了这个代码,但问题是哈希算法是md5,我希望它是sha256(但我找不到一个有用的代码,我可以适应)
#include "md5.h"
#include <string>
#include <iostream>
using namespace std;
int main()
{
MD5 md5 ;
string str1(md5.digestString( "String" ));
cout << str1;
return 0;
}
我已经看了几个for循环,但我似乎可以让他们中的任何一个工作,但这里是我的尝试之一:
int i = 0;
for (; i<50; ++i) {
cout << "Hello world";
}
for循环的错误:
2 1 C:Users 9stephenbDesktopHash.cpp [Error] expected unqualified-id before 'for'
2 8 C:Users 9stephenbDesktopHash.cpp [Error] 'i' does not name a type
2 14 C:Users 9stephenbDesktopHash.cpp [Error] expected unqualified-id before '++' token
但我得到错误,如&;它不是一个类型&;,我假设变量类型,但我不太确定下一步该怎么做。
我现在有点不愿意完全学习c++,因为这只是我正在从事的项目的一小部分。
编辑
形成评论和答案,我有这个代码:
#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;
}
但是它返回错误:[Error] 'md5' was not declared in this scope
循环可以是这样的:
for (int i = 0; i<50; i++) {
cout << "Hello world"
}
sha256检查http://www.cryptlib.com/
添加了"<<"操作符,如Fabio Turati所说。
几乎和你的伪代码一样:)
int count = 50;
std::string cur = "stringtohash";
for(int i=0; i < count; ++i) {
cur = whateverlib.md5.digest(cur );
}
std::cout << cur;
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- C++:将控制台输出存储在宏中更好吗
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- SSH通过/sbin/SSH无法读取RSA密钥文件(从控制台运行)
- 这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号
- boost::asio如何生成多个协同程序,然后加入它们
- C++控制台应用程序阻止退出
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- 如何修复此iFile以将.txt输出到控制台
- 有没有办法将 getline() 与外部文本文件一起使用,然后重新获得 cin 的控制权以从控制台获取输入?
- 有没有办法从程序中打开一个新控制台,然后进入其中
- 如何使用我的 if-else 语句根据用户权重的输入执行特定操作,然后将其显示到控制台?
- 控制台窗口闪烁C++然后消失
- 是否可以将字母 X 放置在控制台中央,以 100 像素/秒的速度下降,然后单击时升高 50 像素
- 控制台输入,直到.然后输入
- 打印到控制台,然后创建文件并将控制台内容打印到文件中
- c++哈希字符串X次,然后写入控制台
- 读取显示到控制台的文本文件,然后追加文本文件
- 从控制台保存信息,然后重新打开时,信息仍然存在