sha.cpp

来自「经典的string 函数库学习资料」· C++ 代码 · 共 53 行

CPP
53
字号
/** * @file sha.cpp * @brief 字符串的包装,sha1单向散列函数 * @author 泥偶 * @since  2003-09-26 * @date   2003-11-03 */#define HAVE_MEMCPY#include "xstring.hpp"namespace x{    namespace    {        #include <string.h>        #include "sha.c"    }   // namespace//----------------------------------------------------------------------------    /**    根据    <a href="http://www.faqs.org/rfcs/rfc3174">US Secure Hash Algorithm 1</a>    计算字符串的sha1散列。这个散列是一个40个字符的十六进制数字。    如果可选的raw_output被设置成true,那将会返回20个字符长度的原始二进制格式。    @code  * xstring str = "apple";  * if ("d0be2dc421be4fcd0172e5afceea3970e2f3d940" == str.sha1())  * {  *     cout << "Would you like a green or red apple?";  * }    @endcode    raw_output参数是1.0.2test3版本新增的    @see crc32() md5()     */    xstring xstring::sha1(bool raw_output) const    {        char digest[20];        sha_buffer(this->data(), this->size(), digest);        xstring str(digest, sizeof(digest));        if (raw_output)        {            return str;        }        return str.bin2hex();    }}   // namespace x

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?