[英]SSHA512 generating hash in c# for Postfix
I have to generate password hash with SSHA512 which could work with Postfix.我必须使用可以与 Postfix 一起使用的 SSHA512 生成密码 hash。 I have hash generator written in Python and i need to rewrite it to C#.我有用 Python 编写的 hash 生成器,我需要将其重写为 C#。 I wrote some code in c# that generating hash but Postfix could not verify generated password.我在 c# 中编写了一些生成 hash 的代码,但 Postfix 无法验证生成的密码。
Python code: 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)
And My C# code version 1:还有我的 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# code version 2: 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());
}
In version 1 generated hash is shorten than hash generated in Python.在版本 1 中生成的 hash 比在 Python 中生成的 hash 短。 In version 2 length is same.在版本 2 中,长度相同。 Unfortunetly both versions doesn't work in Postfix.不幸的是,这两个版本都不能在 Postfix 中工作。 Generated hash can't be verified.生成的 hash 无法验证。
Values i get:我得到的价值观:
Password: texas密码:德州
Python generated password: Python 生成密码:
Q5RlBLOCizu/o/iI7NrzqI1lIvMJ5lLngPH1zdeYSdbA+G+wzNcCTwPOwEG/oKM5P3bqrBcm5gLDSdBO3IjnplWHbgqgTBvV
C#: C#:
raw salt values v1:原盐值 v1:
0xed 0x01 0x97 0x7b 0xc2 0xec 0x29 0x76
encoded salt v1:编码盐v1:
7Q+Xe8LsKXY=
encode salt + pass v1:编码盐+通过v1:
2EXXraKRShKwOOb9TYU4hoXVQPhhujaK2dJVe8/n423tW3dOBBdycUPBluMmRtkNELIocAIkRKR6Rk+R5T43rw==
From the posted result of the Python code从 Python 代码的发布结果
Q5RlBLOCizu/o/iI7NrzqI1lIvMJ5lLngPH1zdeYSdbA+G+wzNcCTwPOwEG/oKM5P3bqrBcm5gLDSdBO3IjnplWHbgqgTBvV
the following can be derived as salt after Base64 decoding: Base64 解码后可以得到以下盐:
55876e0aa04c1bd5 (hex encoded) or VYduCqBMG9U= (Base64 encoded)
In the C# code, first salt and password must be encoded:在 C# 代码中,必须先对盐和密码进行编码:
byte[] salt = Convert.FromBase64String("VYduCqBMG9U=");
byte[] password = Encoding.ASCII.GetBytes("texas"); // Here the encoding of the Python code must be used
Before hashing, both byte[]
must be concatenated.在散列之前,必须连接两个byte[]
。 Since the concatenation of byte[]
is needed again later, it's useful to implement a helper method:由于稍后需要再次连接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;
}
Thus the hashing is performed as follows:因此,哈希执行如下:
HashAlgorithm algorithm = new SHA512Managed();
byte[] passwordSalt = join(password, salt);
byte[] hash = algorithm.ComputeHash(passwordSalt);
Before the Base64 encoding, hash and salt must be concatenated:在 Base64 编码之前,hash 和 salt 必须连接:
byte[] hashSalt = join(hash, salt);
string hashSaltB64 = Convert.ToBase64String(hashSalt); // Q5RlBLOCizu/o/iI7NrzqI1lIvMJ5lLngPH1zdeYSdbA+G+wzNcCTwPOwEG/oKM5P3bqrBcm5gLDSdBO3IjnplWHbgqgTBvV
To process a random salt you can use eg your CreateSaltRaw
method and simply replace要处理随机盐,您可以使用例如您的CreateSaltRaw
方法并简单地替换
byte[] salt = Convert.FromBase64String("VYduCqBMG9U=");
by经过
byte[] salt = CreateSaltRaw(8);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.