.NET 4.x Переписать код с JS на C#
Формулировка задачи:
Есть такой код на JavaScript
a - double
b - string
Как его переписать на шарпе?
function getPoW(a, b) {
var c = 0;
do
hash = eval("(0x" + md5(md5(b + c)) + ")"),
++c;
while (hash >= a);return c
}Решение задачи: «.NET 4.x Переписать код с JS на C#»
textual
Листинг программы
using System;
using System.Globalization;
using System.Numerics;
using System.Text;
using System.Security.Cryptography;
namespace ConsoleApp
{
class MainClass
{
static BigInteger GetDecimalHash(string value, MD5 hashGenerator) {
var sb = new StringBuilder();
foreach (byte b in hashGenerator.ComputeHash(
Encoding.UTF8.GetBytes(value))) {
sb.AppendFormat("{0:x2}", b);
}
return BigInteger.Parse("0" + sb.ToString(), NumberStyles.HexNumber);
}
static int GetPoW(double a, string b) {
int c = 0;
BigInteger hash = 0;
using (MD5 md5 = MD5.Create()) {
do {
BigInteger inner = GetDecimalHash(b + c.ToString(), md5);
hash = GetDecimalHash(inner.ToString("x"), md5);
c++;
} while (hash >= new BigInteger(a));
}
return c;
}
internal static void Main() {
Console.WriteLine(GetPoW(Math.Pow(10.0, 36.0), "Hello"));
}
}
}