簡介
bcrypt是一個(gè)跨平臺的文件加密工具。由它加密的文件可在所有支持的操作系統(tǒng)和處理器上進(jìn)行轉(zhuǎn)移。它的口令必須是8至56個(gè)字符,并將在內(nèi)部被轉(zhuǎn)化為448位的密鑰。
配置流程
本文選用華為鯤鵬云服務(wù)ECS KC1實(shí)例做測試,KC1實(shí)例的處理器為兼容ARMv8指令集的鯤鵬920。
1. 選擇操作環(huán)境
本文選用華為鯤鵬云服務(wù)ECS KC1實(shí)例做測試
2. 安裝前準(zhǔn)備
1)(可選)修改yum命令的相關(guān)文件。
僅在yum命令不能執(zhí)行且系統(tǒng)有安裝多個(gè)版本的Python時(shí)需要執(zhí)行。
在本示例中,由于將系統(tǒng)的“python 2.7”升級到“python 3.5.6”,導(dǎo)致yum命令不能正常使用,需要修改yum命令的相關(guān)文件。
a. 將“/usr/bin/yum”文件的“#!/usr/bin/python”改為“#!/usr/bin/python2.7”。
b. 將“/usr/libexec/urlgrabber-ext-down”文件的“#!/usr/bin/python”改為“#!/usr/bin/python2.7”。
2)安裝依賴包。
bcrypt依賴于libffi,所以先使用yum安裝該依賴包。
yum install libffi-devel
3. 安裝bcrypt
使用pip命令安裝bcrypt。
pip install bcrypt
回顯內(nèi)容如下,表示安裝成功。
Installing collected packages: cffi, six, bcrypt Running setup.py install for cffi ... done Running setup.py install for bcrypt ... done Successfully installed bcrypt-3.1.7 cffi-1.12.3 six-1.12.0
4. 測試已完成安裝的軟件
1)創(chuàng)建“bcrypt_test.py”文件,并添加如下內(nèi)容。
import bcrypt #導(dǎo)入bcrypt模塊 password = "mypassword" #定義password變量,并賦值 用一個(gè)隨機(jī)的鹽值來加密密碼,還可以接受一個(gè)參數(shù)來控制它要計(jì)算多少次,默認(rèn)是 12 magic_value=bcrypt.gensalt() # 使用utf8編碼格式對字符串編碼 pwd = password.encode('utf8') 使用utf8編碼格式對字符串編碼 hashed = bcrypt.hashpw(pwd, magic_value) print ("The The cryptographic value is: ",hashed) 驗(yàn)證密碼和加密密碼的一致性 if bcrypt.checkpw(password.encode('utf8'), hashed): print("They Matches!") else: print("They Do not Match.")
2)進(jìn)入python交互界面,輸入命令。
python bcrypt_test.py 回顯內(nèi)容如下,表示測試成功。 The cryptographic value is: b'$2b$12$lBFnCJHexc8jKySIBnD56ukMNZvbcFeBOgw7TQqxqqHOhUiys yPw.' They Matches!