1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| static string calc_sha256(string graine)
{
SHA256 hash = SHA256.Create();
byte[] Tab = Encoding.UTF8.GetBytes(graine);
byte[] hashResult = hash.ComputeHash(Tab);
// On prend les 8 premiers octets (64 bits de poids fort).
byte[] buffer = new byte[8];
Array.Copy(hashResult, buffer, 8);
Comment sais-tu que tu récupères les bits à poids fort ?
Pour moi tu prends seulemnt les 8 premiers octets, non ?
// On crée un BitArray d'après ce tableau
BitArray bitArray = new BitArray(buffer);
// On parcours les bits pour calculer la valeur entière.
ulong value = 0;
int i = 63; A quoi correspond cette valeur, utilité ?
foreach (bool b in bitArray)
{
if (b)
{
value += (ulong)Math.Pow(2, i);
}
i--;
}
return value.ToString().PadLeft(20, '0');
En sortie on obtiendra pas tout le temps 20 chiffres, pourquoi ?
} |
Partager