Python 3下MD5加密

        <pre class="prettyprint" name="code"><code class="hljs vala has-numbering" onclick="mdcp.copyCode(event)"
                                                   style="position: unset;">
            <span class="hljs-preprocessor"># 由于MD5模块在python3中被移除</span>
            <span class="hljs-preprocessor"># 在python3中使用hashlib模块进行md5操作</span>

import hashlib

# 待加密信息
str = ‘this is a md5 test.’

# 创建md5对象
hl = hashlib.md5()

# Tips
# 此处必须声明encode
# 若写法为hl.update(str) 报错为: Unicode-objects must be encoded before hashing
hl.update(str.encode(encoding=‘utf-8’))

print(‘MD5加密前为 :’ + str)
print(‘MD5加密后为 :’ + hl.hexdigest())

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

        <blockquote>
            <p><strong>运行结果</strong></p>
        </blockquote>

        <p>
            <img src="https://img-blog.csdn.net/20170704143346220?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfODc4Nzk5NTc5/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast"
                 alt="这里写图片描述" title=""></p>

        <blockquote>
            <p><strong>封装Python3下MD5加密</strong></p>
        </blockquote>

        <pre class="prettyprint" name="code"><code class="hljs python has-numbering" onclick="mdcp.copyCode(event)"
                                                   style="position: unset;">

# 生成MD5
def genearteMD5(str):
# 创建md5对象
hl = hashlib.md5()

<span class="hljs-comment"># Tips</span>
<span class="hljs-comment"># 此处必须声明encode</span>
<span class="hljs-comment"># 否则报错为:hl.update(str)    Unicode-objects must be encoded before hashing</span>
hl.update(str.encode(encoding=<span class="hljs-string">'utf-8'</span>))

print(<span class="hljs-string">'MD5加密前为 :'</span> + str)
print(<span class="hljs-string">'MD5加密后为 :'</span> + hl.hexdigest())<div class="hljs-button {2}"
                                                                         data-title="复制"></div></code><ul
                class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li
                style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li
                style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li
                style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li
                style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li
                style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li
                style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li></ul></pre>

        <blockquote>
            <p><strong>Python2版本中带有MD5模块生成MD5 如下</strong></p>
        </blockquote>

        <pre class="prettyprint" name="code"><code class="hljs coffeescript has-numbering"
                                                   onclick="mdcp.copyCode(event)" style="position: unset;"><span
                class="hljs-reserved">import</span> md5

src = ‘this is a md5 test.’
m1 = md5.new()
m1.update(src.encode(encoding=‘utf-8’))
print(m1.hexdigest())

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7