使用python在Windows上创建兼容的ldap密码(md5crypt)

发布于 2024-09-30 07:12:46 字数 310 浏览 1 评论 0原文

你知道如何在 Windows 上通过 python 创建 ldap 兼容密码(首选 md5crypt)吗?

我曾经在 Linux 中编写类似的内容,但 Windows 上不存在 crypt 模块

char_set = string.ascii_uppercase + string.digits
salt = ''.join(random.sample(char_set,8))
salt = '$1$' + salt + '$'
pwd = "{CRYPT}" + crypt.crypt(str(old_password),salt)

Do you know how to create a ldap compatible password (preferred md5crypt) via python on Windows

I used to write something like this in Linux but the crypt module is not present on Windows

char_set = string.ascii_uppercase + string.digits
salt = ''.join(random.sample(char_set,8))
salt = '$1
 + salt + '

pwd = "{CRYPT}" + crypt.crypt(str(old_password),salt)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

御守 2024-10-07 07:12:46

Passlib python 库包含所有 crypt(3) 算法的跨平台实现。特别是,它包含 ldap_md5_crypt,听起来就像什么你想要的。下面是如何使用它(此代码可以在 Windows 或 Linux 上运行):

from passlib.hash import ldap_md5_crypt

#note salt generation is automatically handled
hash = ldap_md5_crypt.encrypt("password")

#hash will be similar to '{CRYPT}$1$wa6OLvW3$uzcIj2Puf3GcFDf2KztQN0'

#to verify a password...
valid = ldap_md5_crypt.verify("password", hash)

我应该注意,虽然 MD5-Crypt 得到了广泛的支持(Linux、所有 BSD、内部在 openssl 中),但它绝对不是可用的最强哈希确实非常不安全,如果可能的话应该避免。如果您想要与 linux crypt() 兼容的最强哈希,SHA512-Crypt 可能是您的最佳选择。它增加了可变回合,以及内部对 MD5-Crypt 的一些其他改进。

The Passlib python library contains cross-platform implementations of all the crypt(3) algorithms. In particular, it contains ldap_md5_crypt, which sounds like exactly what you want. Here's how to use it (this code will work on windows or linux):

from passlib.hash import ldap_md5_crypt

#note salt generation is automatically handled
hash = ldap_md5_crypt.encrypt("password")

#hash will be similar to '{CRYPT}$1$wa6OLvW3$uzcIj2Puf3GcFDf2KztQN0'

#to verify a password...
valid = ldap_md5_crypt.verify("password", hash)

I should note that while MD5-Crypt is widely supported (Linux, all the BSDs, internally in openssl), it's none-the-less not the strongest hash available really horribly insecure, and should be avoided if at all possible. If you want the strongest hash that's compatible with linux crypt(), SHA512-Crypt is probably the way to go. It adds variable rounds, as well as some other improvements over MD5-Crypt internally.

阳光的暖冬 2024-10-07 07:12:46

从这里 http://www.openldap.org/faq/data/cache/ 347.html

用于生成 SHA 哈希值的变体之一可以是:

import sha 
from base64 import b64encode 

ctx = sha.new("your_password") 
hash = "{SHA}" + b64encode(ctx.digest())
print(hash)

此代码适用于 Python。

# python my_sha.py
{SHA}Vk40DNSEN9Lf6HbuFUzJncTQ0Tc=

我(不仅是我)不再建议使用 MD5。

附言。点击链接您可以尝试一些 Windows 变体。

From here http://www.openldap.org/faq/data/cache/347.html

One of the variants for generating SHA-hash can be:

import sha 
from base64 import b64encode 

ctx = sha.new("your_password") 
hash = "{SHA}" + b64encode(ctx.digest())
print(hash)

This code is for Python.

# python my_sha.py
{SHA}Vk40DNSEN9Lf6HbuFUzJncTQ0Tc=

I (and not only me) don't recommend to use MD5 anymore.

PS. Follow the link you can try some windows variants.

◇流星雨 2024-10-07 07:12:46

您需要使用 fcrypt,这是一个纯 Python 实现Unix 模块crypt 的一部分。它比 crypt 慢一点,但具有相同的功能。

You'll want to use fcrypt, which is a pure Python implementation of the Unix module crypt. It's a bit slower than crypt but it has the same functionality.

素罗衫 2024-10-07 07:12:46

免责声明:我了解谷歌,但不了解密码学。

来自 crypt 文档

该模块实现了一个接口
crypt(3) 例程,这是一个
基于单向哈希函数
改进的DES算法;参见 Unix
手册页以获取更多详细信息。可能的
用途包括允许 Python 脚本
接受来自键入的密码
用户,或试图破解 Unix
用字典输入密码。

您可以查看 md5crypt.py。或者,crypt for WindowsGnuWin32。这是 Unix 手册页的一些内容; Windows 界面应该类似。

加密(3) Linux
程序员手册
地穴(3)

姓名
crypt, crypt_r - 密码和数据加密

概要

<前><代码> #define _XOPEN_SOURCE
#include;

char *crypt(const char *key, const char *salt);

char *crypt_r(const char *key, const char *salt,
结构 crypt_data *数据);

与 -lcrypt 链接。

描述

crypt()是密码加密函数。它是基于
数据
具有预期变化的加密标准算法(其中
其他
事物)阻止使用密钥的硬件实现
搜索。

key 是用户输入的密码。

salt 是从 [a–zA–Z0–9./] 集合中选择的两个字符的字符串。

字符串用于扰乱 4096 个不同的算法之一
方法。

通过取前八个字符中每个字符的最低 7 位

密钥,获得56位密钥。这个 56 位密钥用于
加密
重复一个常量字符串(通常是由以下组成的字符串)
全部
零)。返回值指向加密后的密码,a
系列
13 个可打印 ASCII 字符(前两个字符
代表
盐本身)。返回值指向静态数据,其
内容
被每次调用覆盖。

警告:密钥空间可能由 2**56 等于 7.2e16 组成
价值观。
可以使用大量的方法对该关键空间进行详尽的搜索
帕-
等位基因计算机。可以使用破解(1)等软件
将要
搜索该密钥空间中通常被使用的部分
人类
用于密码。因此,密码选择至少应该:
避免
常用词和名称。使用 passwd(1) 程序进行检查
为了
建议在选择过程中使用可破解的密码。

DES 算法本身有一些怪癖,需要使用

crypt() 接口除了
密码
验证。如果您打算使用 crypt()
接口用于
密码学项目,不要这样做:获取一本关于加密的好书

广泛使用的 DES 库之一。

Disclaimer: I know Google, not cryptography.

From the crypt docs:

This module implements an interface to
the crypt(3) routine, which is a
one-way hash function based upon a
modified DES algorithm; see the Unix
man page for further details. Possible
uses include allowing Python scripts
to accept typed passwords from the
user, or attempting to crack Unix
passwords with a dictionary.

You could have a look at md5crypt.py. Alternatively, crypt for Windows is part of GnuWin32. Here's some of the Unix man page; the Windows interface should be similar.

CRYPT(3) Linux
Programmer's Manual
CRYPT(3)

NAME
crypt, crypt_r - password and data encryption

SYNOPSIS

   #define _XOPEN_SOURCE
   #include <unistd.h>

   char *crypt(const char *key, const char *salt);

   char *crypt_r(const char *key, const char *salt,
                 struct crypt_data *data);

Link with -lcrypt.

DESCRIPTION

crypt() is the password encryption function. It is based on
the Data
Encryption Standard algorithm with variations intended (among
other
things) to discourage use of hardware implementations of a key
search.

key is a user's typed password.

salt is a two-character string chosen from the set [a–zA–Z0–9./].
This
string is used to perturb the algorithm in one of 4096 different
ways.

By taking the lowest 7 bits of each of the first eight characters
of
the key, a 56-bit key is obtained. This 56-bit key is used to
encrypt
repeatedly a constant string (usually a string consisting of
all
zeros). The returned value points to the encrypted password, a
series
of 13 printable ASCII characters (the first two characters
represent
the salt itself). The return value points to static data whose
content
is overwritten by each call.

Warning: The key space consists of 2**56 equal 7.2e16 possible
values.
Exhaustive searches of this key space are possible using massively
par‐
allel computers. Software, such as crack(1), is available which
will
search the portion of this key space that is generally used by
humans
for passwords. Hence, password selection should, at minimum,
avoid
common words and names. The use of a passwd(1) program that checks
for
crackable passwords during the selection process is recommended.

The DES algorithm itself has a few quirks which make the use of
the
crypt() interface a very poor choice for anything other than
password
authentication. If you are planning on using the crypt()
interface for
a cryptography project, don't do it: get a good book on encryption
and
one of the widely available DES libraries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文