SHA1の計算をboostで簡単にできないかなぁと思って調べたら簡単に出来たのでメモ。
boost/uuid/sha1.hppにそのものズバリがありました。
なんてboostは便利なんでしょう(感嘆)
sha1test.cpp
#include <iostream>
#include <boost/uuid/sha1.hpp>
#include <boost/cstdint.hpp>
#include <boost/array.hpp>
#include <stdio.h>
typedef boost::array<boost::uint8_t,20> hash_array;
// SHA1を計算し、uint8型の配列に入れる
void calc_sha1_hash(const void* data,size_t count,hash_array &hash)
{
boost::uuids::detail::sha1 sha1;
unsigned int inthash[5];
sha1.process_bytes(data,count);
sha1.get_digest(inthash);
// このままでも使えるけど、uint8にしたほうがいろいろと都合が良いので
for(int i=0;i<5;i++){
hash[i*4] = (inthash[i] & 0xFF000000) >> 24;
hash[i*4+1] = (inthash[i] & 0x00FF0000) >> 16;
hash[i*4+2] = (inthash[i] & 0x0000FF00) >> 8;
hash[i*4+3] = (inthash[i] & 0x000000FF) ;
}
}
void hash_disp(boost::uint8_t x)
{
printf("%02X",x);
}
int main(void)
{
hash_array hasher;
calc_sha1_hash("abcd",4,hasher);
std::for_each(hasher.begin(),hasher.end(),hash_disp);
return 0;
}
sha1.hppのhash配列がint[5]固定で何かと使いにくいので、その辺は変換関数作って各自対処ってことなんですかね。