[英]SSHA512 generating hash in c# for Postfix
我必须使用可以与 Postfix 一起使用的 SSHA512 生成密码 hash。 我有用 Python 编写的 hash 生成器,我需要将其重写为 C#。 我在 c# 中编写了一些生成 hash 的代码,但 Postfix 无法验证生成的密码。
Python代码:
def generate_ssha512_password(p):
"""Generate salted SHA512 password with prefix '{SSHA512}'.
Return SSHA instead if python is older than 2.5 (not supported in module hashlib)."""
p = str(p).strip()
try:
from hashlib import sha512
salt = os.urandom(8)
pw = sha512(p)
pw.update(salt)
return '{SSHA512}' + b64encode(pw.digest() + salt)
还有我的 C# 代码版本 1:
public static string CreateSalt(int size)
{
//Generate a cryptographic random number.
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] buff = new byte[size];
rng.GetBytes(buff);
return Convert.ToBase64String(buff);
}
public static string GenerateHash(string input, string salt)
{
byte[] bytes = Encoding.UTF8.GetBytes(input + salt);
SHA512 sHA512ManagedString = new SHA512Managed();
byte[] hash = sHA512ManagedString.ComputeHash(bytes);
return Convert.ToBase64String(hash);
}
C#代码版本2:
public static byte[] CreateSaltRaw(int size)
{
//Generate a cryptographic random number.
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] buff = new byte[size];
rng.GetBytes(buff);
return buff;
}
public static string GenerateHash2(string input, byte[] salt)
{
byte[] bytes = Encoding.UTF8.GetBytes(input + Convert.ToBase64String(salt));
SHA512 sHA512ManagedString = new SHA512Managed();
byte[] hash = sHA512ManagedString.ComputeHash(bytes);
List<byte> pass = hash.ToList();
foreach (var s in salt)
{
pass.Add(s);
}
//return Convert.ToBase64String(hash);
return Convert.ToBase64String(pass.ToArray());
}
在版本 1 中生成的 hash 比在 Python 中生成的 hash 短。 在版本 2 中,长度相同。 不幸的是,这两个版本都不能在 Postfix 中工作。 生成的 hash 无法验证。
我得到的价值观:
密码:德州
Python 生成密码:
Q5RlBLOCizu/o/iI7NrzqI1lIvMJ5lLngPH1zdeYSdbA+G+wzNcCTwPOwEG/oKM5P3bqrBcm5gLDSdBO3IjnplWHbgqgTBvV
C#:
原盐值 v1:
0xed 0x01 0x97 0x7b 0xc2 0xec 0x29 0x76
编码盐v1:
7Q+Xe8LsKXY=
编码盐+通过v1:
2EXXraKRShKwOOb9TYU4hoXVQPhhujaK2dJVe8/n423tW3dOBBdycUPBluMmRtkNELIocAIkRKR6Rk+R5T43rw==
从 Python 代码的发布结果
Q5RlBLOCizu/o/iI7NrzqI1lIvMJ5lLngPH1zdeYSdbA+G+wzNcCTwPOwEG/oKM5P3bqrBcm5gLDSdBO3IjnplWHbgqgTBvV
Base64 解码后可以得到以下盐:
55876e0aa04c1bd5 (hex encoded) or VYduCqBMG9U= (Base64 encoded)
在 C# 代码中,必须先对盐和密码进行编码:
byte[] salt = Convert.FromBase64String("VYduCqBMG9U=");
byte[] password = Encoding.ASCII.GetBytes("texas"); // Here the encoding of the Python code must be used
在散列之前,必须连接两个byte[]
。 由于稍后需要再次连接byte[]
,因此实现辅助方法很有用:
private static byte[] join(byte[] b1, byte[] b2)
{
byte[] b = new byte[b1.Length + b2.Length];
Buffer.BlockCopy(b1, 0, b, 0, b1.Length);
Buffer.BlockCopy(b2, 0, b, b1.Length, b2.Length);
return b;
}
因此,哈希执行如下:
HashAlgorithm algorithm = new SHA512Managed();
byte[] passwordSalt = join(password, salt);
byte[] hash = algorithm.ComputeHash(passwordSalt);
在 Base64 编码之前,hash 和 salt 必须连接:
byte[] hashSalt = join(hash, salt);
string hashSaltB64 = Convert.ToBase64String(hashSalt); // Q5RlBLOCizu/o/iI7NrzqI1lIvMJ5lLngPH1zdeYSdbA+G+wzNcCTwPOwEG/oKM5P3bqrBcm5gLDSdBO3IjnplWHbgqgTBvV
要处理随机盐,您可以使用例如您的CreateSaltRaw
方法并简单地替换
byte[] salt = Convert.FromBase64String("VYduCqBMG9U=");
经过
byte[] salt = CreateSaltRaw(8);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.