Files

75 lines
4.1 KiB
C#

using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
// SHA-512 crypt ($6$), not a plain SHA-512 digest. Algorithm:
// https://www.akkadia.org/drepper/SHA-crypt.txt
namespace Staging {
public static class Sha512Crypt {
private const string Alphabet = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
private static byte[] Hash(params byte[][] parts) {
using (var stream = new MemoryStream()) {
foreach (var part in parts) stream.Write(part, 0, part.Length);
var bytes = stream.ToArray();
try { using (var sha = SHA512.Create()) return sha.ComputeHash(bytes); }
finally { Array.Clear(bytes, 0, bytes.Length); }
}
}
private static byte[] Repeat(byte[] bytes, int length) {
var result = new byte[length];
for (int i = 0; i < length; i++) result[i] = bytes[i % bytes.Length];
return result;
}
private static void Encode(StringBuilder result, int a, int b, int c, int count) {
int value = (a << 16) | (b << 8) | c;
while (count-- > 0) { result.Append(Alphabet[value & 63]); value >>= 6; }
}
public static string NewSalt() {
var bytes = new byte[16];
using (var rng = RandomNumberGenerator.Create()) rng.GetBytes(bytes);
var chars = new char[16];
for (int i = 0; i < chars.Length; i++) chars[i] = Alphabet[bytes[i] & 63];
return new string(chars);
}
public static string Compute(string password, string salt, int rounds) {
if (String.IsNullOrEmpty(password) || password.Length > 256) throw new ArgumentException("Password length");
if (String.IsNullOrEmpty(salt) || salt.Length > 16) throw new ArgumentException("Salt length");
foreach (char c in salt) if (Alphabet.IndexOf(c) < 0) throw new ArgumentException("Salt character");
if (rounds < 1000 || rounds > 1000000) throw new ArgumentException("Rounds");
byte[] p = Encoding.UTF8.GetBytes(password), s = Encoding.ASCII.GetBytes(salt);
byte[] repeated = null, dp = null, ds = null, a = null;
try {
var b = Hash(p, s, p);
using (var stream = new MemoryStream()) {
stream.Write(p, 0, p.Length); stream.Write(s, 0, s.Length);
var chunk = Repeat(b, p.Length); stream.Write(chunk, 0, chunk.Length); Array.Clear(chunk, 0, chunk.Length);
for (int n = p.Length; n > 0; n >>= 1) {
var bytes = (n & 1) != 0 ? b : p; stream.Write(bytes, 0, bytes.Length);
}
var input = stream.ToArray(); a = Hash(input); Array.Clear(input, 0, input.Length);
}
Array.Clear(b, 0, b.Length);
repeated = Repeat(p, p.Length * p.Length); var digest = Hash(repeated);
dp = Repeat(digest, p.Length); Array.Clear(repeated, 0, repeated.Length); Array.Clear(digest, 0, digest.Length);
repeated = Repeat(s, s.Length * (16 + a[0])); digest = Hash(repeated);
ds = Repeat(digest, s.Length); Array.Clear(repeated, 0, repeated.Length); Array.Clear(digest, 0, digest.Length);
for (int i = 0; i < rounds; i++) {
byte[] previous = a;
a = Hash((i & 1) != 0 ? dp : previous, i % 3 != 0 ? ds : new byte[0],
i % 7 != 0 ? dp : new byte[0], (i & 1) != 0 ? previous : dp);
Array.Clear(previous, 0, previous.Length);
}
var output = new StringBuilder("$6$rounds=" + rounds + "$" + salt + "$");
for (int i = 0, j = 0; j < 21; j++, i = (i + 22) % 63)
Encode(output, a[i], a[(i + 21) % 63], a[(i + 42) % 63], 4);
Encode(output, 0, 0, a[63], 2);
return output.ToString();
} finally {
foreach (var bytes in new[] { p, s, repeated, dp, ds, a })
if (bytes != null) Array.Clear(bytes, 0, bytes.Length);
}
}
}
}