Python使用Apply或Map计算NTLM哈希值

时间:2018-12-06 21:32:40

标签: python pandas apply

我正在使用密码,并且想计算密码的NTLM哈希值,因此我测试了hashlib和binascii来对密码进行哈希处理,并且它可以正常工作。请参阅以下内容:

 import hashlib,binascii

 hash = hashlib.new('md4', "thisisastrongpassword".encode('utf-16le')).digest()
 password = "thisisastrongpassword"
 print "Password before hash: ",password
 print "Hashed password: ",binascii.hexlify(hash)

输出:

 Password before hash:  thisisastrongpassword
 Hashed password:  912ed96fab781cce35f3b700cff7123c

我正在尝试对存储在具有一列的数据框中的大型数据集进行尝试。我不需要打印语句,我想要的只是哈希值。有没有使用map或在以下df上应用的好方法?

df1
 column_1
 Jimbob@@$
 Jimbob@@$
 Jimbob@@%
 Jimbob@@%
 Jimbob@@&
 Jimbob@@*
 Jimbob@@@
 Jimbob@@@
 JimbobJimmy
 JimbobJimmy
 Jimbob_005
 Jimbob_005
 Jimbob_0053
 Jimbob_0053
 Jimbob_0055

1 个答案:

答案 0 :(得分:1)

您只需定义一个函数即可散列密码并使用applymap

def hasher(p):
    hash_ = hashlib.new('md4', p.encode('utf-16le')).digest()
    return binascii.hexlify(hash_)

df.column_1.apply(hasher)
# or 
df.column_1.map(hasher)

测试:

>>> hasher('thisisastrongpassword')
# b'912ed96fab781cce35f3b700cff7123c'

>>> df.column_1.apply(hasher)
0     b'2b1349acddf4ad6802f18d3a6abaa84f'
1     b'2b1349acddf4ad6802f18d3a6abaa84f'
2     b'c36b0a1847b70be9c6746fcd5de3a916'
3     b'c36b0a1847b70be9c6746fcd5de3a916'
4     b'5cb8d96c0f2ed5c7f758379c875cdf79'
5     b'c11549aa7b217a141b51b74eaed0fbdf'
6     b'3590ebf6c9c1380ab8c9648bed6ce2c8'
7     b'3590ebf6c9c1380ab8c9648bed6ce2c8'
8     b'5e6de5fad20db51b594197d6b0213136'
9     b'5e6de5fad20db51b594197d6b0213136'
10    b'719b5aa4e400c02ad401f324262214c2'
11    b'719b5aa4e400c02ad401f324262214c2'
12    b'd6524a455013f156082a278d14b311d4'
13    b'd6524a455013f156082a278d14b311d4'
14    b'98cb6c0caa0bc159d8a10b7387c06caa'
Name: column_1, dtype: object