Rya*_*anW 1 .net c# python asp.net unicode
我正在研究一个Python脚本来从类似于ASP.NET的MembershipProvider的现有系统创建散列字符串.使用Python,有没有办法获取十六进制字符串并将其转换回二进制文件然后执行base64编码,以某种方式将原始字符串视为Unicode.我们来试试吧.我想重新编码一个哈希密码,以便在Python和ASP.NET/C#中哈希值相等:
import base64
import sha
import binascii
def EncodePassword(password):
# strings are currently stored as hex
hex_hashed_password = sha.sha(password).hexdigest()
# attempt to convert hex to base64
bin_hashed_password = binascii.unhexlify(hex_hashed_password)
return base64.standard_b64encode(bin_hashed_password)
print EncodePassword("password")
# W6ph5Mm5Pz8GgiULbPgzG37mj9g=
Run Code Online (Sandbox Code Playgroud)
ASP.NET MembershipProvider使用此方法进行编码:
static string EncodePassword(string pass)
{
byte[] bytes = Encoding.Unicode.GetBytes(pass);
//bytes = Encoding.ASCII.GetBytes(pass);
byte[] inArray = null;
HashAlgorithm algorithm = HashAlgorithm.Create("SHA1");
inArray = algorithm.ComputeHash(bytes);
return Convert.ToBase64String(inArray);
}
string s = EncodePassword("password");
// 6Pl/upEE0epQR5SObftn+s2fW3M=
Run Code Online (Sandbox Code Playgroud)
那不匹配.但是,当我使用ASCII编码编码的密码运行它时,它匹配,所以.NET方法的Unicode部分是有区别的.
W6ph5Mm5Pz8GgiULbPgzG37mj9g =
在python脚本中是否有一种方法可以获得与默认.NET版本匹配的输出?
这是诀窍:
Encoding.Unicode
对于UTF-16LE而言,"Unicode"编码令微软感到困惑(具体而言,没有任何BOM).将字符串编码为散列之前的字符串,您将得到正确的答案:
>>> import hashlib
>>> p= u'password'
>>> hashlib.sha1(p.encode('utf-16le')).digest().encode('base64')
'6Pl/upEE0epQR5SObftn+s2fW3M=\n'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
847 次 |
| 最近记录: |