D语言新手问题 D语言获取文件sha1值

· Created · Last modified by Brian replied at · 1538 times read

toHexString() 可以设置默认返回全小写:

string str = toHexString!(LetterCase.lower)(sha);

D语言获取文件sha1值 方法实例

// filename 文件路径  
string getFileHash1(string filename)
{
    import std.digest.digest;
    import std.digest.sha;
    import std.string;
    import std.stdio;

    File file = File(filename,"r");
    SHA1 ahs = SHA1();
    ahs.start();
    ubyte[] data = new ubyte[4 * 1024];
    scope(exit){
        import core.memory;
        GC.free(data.ptr);
    }
    while(!file.eof){
        auto tdata = file.rawRead(data);
        ahs.put(tdata);
    }
    auto sha  = ahs.finish;
    auto str = toHexString(sha);
    string tstr = cast(string)(str[]);
    return toLower(tstr);
}


import hunt.framework.file.UploadedFile;
UploadedFile file = request.file("file");
string sha1_str = getFileHash1(file.path); //9b13fac800f5fb758f5c5e6ffb0d46e196d4ef92
Login to reply