linux生成sha512密码,python生成shadow中密码(SHA512)
在很久很久以前写过一篇文章讲linux中shadow文件的数据构成(https://blog.csdn.net/cracker_zhou/article/details/50817743)。
当然在文章末尾提到了使用python的crypt模块可以生成或者校验linux密码的有效性。但是crypt模块在windows上并不能使用。同时在文章中提交密码的生成依赖于算法(md5,sha512之类)和盐值(salt),但是并没有说清楚salt是如何使用的。
研究发现,密码的生成比我想象的复杂的多,以$6为例并不是单纯的sha512(pass+salt)或者sha512(salt+passwd),而是经历了一系列复杂的运算而成。
下面python代码参考了node版本: https://github.com/mvo5/sha512crypt-node
同时给出官方的C语言版本: https://www.akkadia.org/drepper/SHA-crypt.txt
Tip: 测试代码在python3.6版本编译通过,其他python版本需要做微调
import hashlib,math
def rstr_sha512(text: bytes) -> bytes:
sha512 = hashlib.sha512()
sha512.update(text)
return sha512.digest()
def _extend(source: bytes, size_ref: int) -> bytes :
extended = b"";
for i in range(math.floor(size_ref/64)):
extended += source;
extended += source[:size_ref % 64]
return extended;
def _sha512crypt_intermediate(password: bytes,salt: bytes) -> bytes:
#digest_a = rstr_sha512(password + salt)
digest_b = rstr_sha512(password + salt + password)
digest_b_extended = _extend(digest_b,len(password))
intermediate_input = password + salt + digest_b_extended
passwd_len = len(password)
while passwd_len!=0:
if passwd_len&1 == 1:
intermediate_input += digest_b
else:
intermediate_input += password
passwd_len >>= 1
return rstr_sha512(intermediate_input)
def _sha512crypt(password :bytes,salt :bytes,rounds :int) -> bytes:
digest_a = _sha512crypt_intermediate(password, salt)
p = _extend(rstr_sha512(password*len(password)),len(password))
s = _extend(rstr_sha512(salt*(16+digest_a[0])),len(salt))
digest = digest_a
for i in range(rounds):
c_input = b""
if i&1 :
c_input += p
else:
c_input += digest
if i % 3:
c_input += s
if i % 7:
c_input += p
if i & 1:
c_input += digest
else:
c_input += p
digest = rstr_sha512(c_input)
return digest
def sha512crypt(password :bytes,salt :bytes, rounds=5000) -> str:
salt = salt[:16] # max 16 bytes for salt
input = _sha512crypt(password, salt, rounds)
tab = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
order = [ 42, 21, 0, 1, 43, 22, 23, 2, 44, 45, 24, 3,
4, 46, 25, 26, 5, 47, 48, 27, 6, 7, 49, 28,
29, 8, 50, 51, 30, 9, 10, 52, 31, 32, 11, 53,
54, 33, 12, 13, 55, 34, 35, 14, 56, 57, 36, 15,
16, 58, 37, 38, 17, 59, 60, 39, 18, 19, 61, 40,
41, 20, 62, 63]
output = ""
for i in range(0,len(input),3):
# special case for the end of the input
if i+1 >= len(order): # i == 63
char_1 = input[order[i+0]] & 0b00111111
char_2 = (input[order[i+0]] & 0b11000000) >> 6
output += tab[char_1] + tab[char_2]
else:
char_1 = input[order[i+0]] & 0b00111111
char_2 = (((input[order[i+0]] & 0b11000000) >> 6) |
(input[order[i+1]] & 0b00001111) << 2)
char_3 = (
((input[order[i+1]] & 0b11110000) >> 4) |
(input[order[i+2]] & 0b00000011) << 4)
char_4 = (input[order[i+2]] & 0b11111100) >> 2
output += tab[char_1] + tab[char_2] + tab[char_3] + tab[char_4]
if rounds!=5000:
return "$6$rounds={}${}${}".format(rounds,salt.decode("utf-8"),output)
else:
return "$6${}${}".format(salt.decode("utf-8"),output)
if __name__ == "__main__":
# 与crypt.crypt("123456","$6$123456") 运算结果一致
print(sha512crypt(b"123456",b"123456",5000))
linux生成sha512密码,python生成shadow中密码(SHA512)相关推荐
- python自动生成html_PyH : python生成html
样例 下面是官网的一个例子: from pyh import * page = PyH('My wonderful PyH page') page.addCSS('myStylesheet1.css' ...
- python克里金生成asc_使用Python生成ASCII字符画
使用Python生成ASCII字符画 在很多的网站主页中或者程序的注释中会有一些好看的字符注释画.显得很牛逼的样子 例如: 知乎 _____ _____ _____ _____ /\ \ /\ \ / ...
- linux python matplotlib 使用,关于Linux:如何在Python的matplotlib中设置“后端”?
我是matplotlib的新用户,我的平台是Ubuntu 10.04 Python 2.6.5 这是我的代码 import matplotlib matplotlib.use('Agg') impor ...
- 咨询报告生成:使用python生成pptx格式的报告
已经有人做了,思路和自己想做的一个模式差不多,见模式一: 先做好ppt模板 用python计算数据,生成图形 用python调用模板,将图形插入pptx页面中,生成分析报告 模式二与模式一的差别在第三 ...
- python表白代码照片墙-python入会生成照片墙 利用python生成照片墙代码
本篇文章小编给大家分享一下利用python生成照片墙代码,对大家的学习有一定的帮助,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看. PIL(Python Image Libr ...
- 动态照片墙 python 实现_python入会生成照片墙 利用python生成照片墙代码
本篇文章小编给大家分享一下利用python生成照片墙代码,对大家的学习有一定的帮助,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看. PIL(Python Image Libr ...
- c语言指针生成numpy数组,python – 在cython中声明numpy数组和c指针
在我的代码中,我通常使用numpy数组来连接方法和类.优化我的程序的核心部分我使用cython与那些numpy数组的c指针.不幸的是,我目前正在声明阵列的方式很长. 例如,假设我有一个方法应该返回一个 ...
- python生成excel表格-Python生成excel表格并设置样式
python在做爬虫时会涉及到数据存储问题,下面说一下将数据存储在excel表格中,主要使用扩展类xlwt,下面详细说一下,主要涉及到了数据列,背景,名称等参数. 说明:python3.7.windo ...
- python生成折线图-python 生成图表
# -*- coding:utf-8 -*- import xlsxwriter # 创建一个excel workbook = xlsxwriter.Workbook("chart_pie. ...
最新文章
- c语言测试代码怎么写,初学C语言,写了一个测试手速的工具,代码发上来,存着。。...
- Oracle 12c 新特性之 temp undo
- 【网络安全威胁】企业风险远不止勒索软件,盘点当今企业面临的四种安全威胁
- 3.产品成本在完工和在制产品间分配
- 病的不轻?教你 2 招,拯救拖延症!
- 【渝粤题库】陕西师范大学201281 民法作业
- 定位pure virtual method called问题
- 如何系统性地保障软件性能
- 分公司部署加速设备实现广域网加速
- git 初始化git存储库_什么不保存到Git存储库中
- java字符串查找算法_java – 查找所有“字符相等”字符串的高效算法?
- CentOS 挂载数据盘
- 股票历史数据-股票历史数据下载
- 场景编辑器 Scene Building
- webservice学习wsdl解读(2)
- Unity编辑器修改图片的大小
- Android Studio 一个工程打包多个不同包名的APK
- 京东商城注册页面使用正则表达式,可以用在别处哦
- 2018微信公开课:微信小游戏的精华内容要点分享!
- Smoothness 平滑度 Standard Shader系列9
热门文章
- Qt 嵌入浏览器 QWebEngineView实现浏览器基本功能
- 江南爱窗帘十大品牌 教你穿好窗帘这件外衣
- Redis有序集合命令ZRANGEBYLEX详解与应用
- 希尔排序算法(C语言实现)
- 【C语言】 递归函数DigitSum(n)
- 调用别人的接口的几种方法
- 微信红包封面作者审核流程-免费红包封面序列号领取
- 品优购项目学习记录--01公共模块制作
- jquery获取最外层(最顶部)window对象
- oppoa57的计算机颜色,【OPPOA57评测】ColorOS简单易用 隐私保护好_OPPO A57_手机评测-中关村在线...