SHA-1
Abstract
sha1()
calculate SHA-1 cryptographic hash function.
sha1()
は暗号学的ハッシュ関数である SHA-1 を計算します.
Header file
namespace sstd{
std::vector<uint8> sha1(const uchar* in, size_t in_len);
std::vector<uint8> sha1(const std::vector<uint8>& in);
}
Usage
- main.cpp
#include <sstd/sstd.hpp> std::string uint8_to_str(const std::vector<uint8>& v){ std::string s; for(uint i=0; i<v.size(); ++i){ s += sstd::ssprintf("%02x", (int)v[i]); } return s; } int main(){ sstd::mkdir("./tmp"); sstd::system("head -c 5m /dev/urandom > ./tmp/rand.txt"); // generate 5 MB random file sstd::system("sha1sum ./tmp/rand.txt"); std::vector<uint8> vBin = sstd::read_bin("./tmp/rand.txt"); // read all as a binary std::string md5_str = uint8_to_str( sstd::sha1( vBin ) ); sstd::printn( md5_str ); sstd::rm("./tmp"); }
- Execution result
2a04535a34d20ce5c112d466af949bdcac3c3d29 ./tmp/rand.txt md5_str = "2a04535a34d20ce5c112d466af949bdcac3c3d29"