python模組——hashlib模組(簡單檔案摘要演算法實現)

來源:互聯網
上載者:User

標籤:ash   read   put   path   python   ==   type   sha1   sage   

#!/usr/bin/env python# -*- coding:utf-8 -*-__author__ = "loki"# Usage: hashlib模組import hashlibimport time# wrapper function calc length and use timedef len_type_tools(res):    def wrapper(*args, **kwargs):        cur_time = time.time()        len_res = len(res(*args, **kwargs))        result = res(*args, **kwargs)        end_time = time.time() - cur_time        print(‘Primary HASH result length is {0}, use time {1:.2f}‘.format(len_res, end_time))        return result    return wrapperdef menu_list():    func_list = {}    for item in hashlib.__dict__:        if str(item).startswith("sha") or str(item).startswith("md5"):            func_list[item] = hashlib.__dict__[item]    return func_list@len_type_toolsdef algol(data, num):    if num != 1:        user_choose = menu_list()[data[1]]()        user_choose.update(data[0].encode("utf-8"))        return user_choose.hexdigest()    user_choose = menu_list()["md5"]()    user_choose.update(data[0].encode("utf-8"))    return user_choose.hexdigest()def file_read(path):    with open(r"{}".format(path), ‘rb‘) as file_info:        file_bin = file_info.read()        return file_bindef main():    """    .::hashlib small tools::.        by: loki 2018-06-25    format:    >> <file_path> [algol]    algol:    md5(default)    sha1    sha224    sha256    sha384    sha512    example:    >> c:\\test.txt sha1    :return hash_value    """    print(main.__doc__)    file_path = input(‘>>‘).strip()    file_path = file_path.split()    argument_num = len(file_path)    print(algol(file_path, argument_num))if __name__ == ‘__main__‘:    main()    time.sleep(15)

 

小結:

  再這個hashlib模組學習中,順便回顧了下裝飾器。也運用了下魔術方法__dict__功能

python模組——hashlib模組(簡單檔案摘要演算法實現)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.