Не удается преобразовать из "string" в "Byte[]" - C#

Узнай цену своей работы

Формулировка задачи:

Добрый день. Есть хэширование SHA1. Но, в строчке

string hash = sha1.Hash(text);

выбивает подобную ошибку: "Не удается преобразовать из "string" в "Byte[]"" Можно просто преобразовать, но не уверен что это будет правильно. Прошу помощи! Заранее спасибо!
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Security.Cryptography;
  6. namespace ConsoleApplication5
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. //var text = args[0];
  13. string text = "Николай";
  14. string hash = sha1.Hash(text);
  15. Console.WriteLine("Хэшированное слово:\n{0}", hash);
  16. Console.ReadLine();
  17. }
  18. }
  19. static class sha1
  20. {
  21. static byte[] pad(byte[] mes)
  22. {
  23. byte[] newMes = new byte[((mes.LongLength * 8 + 512 - 447) / 512 + 1) * 512 / 8];
  24. for (long i = 0; i < mes.LongLength; i++)
  25. {
  26. newMes[i] = mes[i];
  27. }
  28. newMes[mes.LongLength] = (1 << 7);
  29. ulong l = (ulong)mes.LongLength * 8;
  30. for (int i = 0; i < 8; i++)
  31. {//write l to end of padding
  32. newMes[newMes.LongLength - i - 1] = (byte)(l >> (i * 8));
  33. }
  34. return newMes;
  35. }
  36. static uint ch(uint x, uint y, uint z)
  37. {
  38. return (x & y) ^ ((~x) & z);
  39. }
  40. static uint parity(uint x, uint y, uint z)
  41. {
  42. return x ^ y ^ z;
  43. }
  44. static uint maj(uint x, uint y, uint z)
  45. {
  46. return (x & y) ^ (x & z) ^ (y & z);
  47. }
  48. static uint ft(uint x, uint y, uint z, int t)
  49. {
  50. if (t <= 19)
  51. return ch(x, y, z);
  52. if (t <= 39)
  53. return parity(x, y, z);
  54. if (t <= 59)
  55. return maj(x, y, z);
  56. return parity(x, y, z);
  57. }
  58. static uint rotl(uint a, int n)
  59. {
  60. return (a << n) | (a >> (32 - n));
  61. }
  62. static uint kt(int t)
  63. {
  64. if (t <= 19)
  65. return 0x5a827999;
  66. if (t <= 39)
  67. return 0x6ed9eba1;
  68. if (t <= 59)
  69. return 0x8f1bbcdc;
  70. return 0xca62c1d6;
  71. }
  72. static uint wt(byte[] mes, int i, int t)
  73. {
  74. if (t <= 15)
  75. return mes.Skip(i * 512 / 8 + t * 4).Take(4).Aggregate((uint)0, (cur, next) =>
  76. (cur << 8) | next);
  77. return rotl(wt(mes, i, t - 3) ^ wt(mes, i, t - 8) ^ wt(mes, i, t - 14) ^
  78. wt(mes, i, t - 16), 1);
  79. }
  80. public static uint[] Hash(byte[] mes)
  81. {
  82. mes = pad(mes);
  83. uint a = 0x67452301;
  84. uint b = 0xefcdab89;
  85. uint c = 0x98badcfe;
  86. uint d = 0x10325476;
  87. uint e = 0xc3d2e1f0;
  88. for (int i = 0; i < mes.LongLength / 64; i++)
  89. {
  90. uint a1 = a;
  91. uint b1 = b;
  92. uint c1 = c;
  93. uint d1 = d;
  94. uint e1 = e;
  95. for (int t = 0; t <= 79; t++)
  96. {
  97. uint T = rotl(a, 5) + ft(b, c, d, t) + e + kt(t) + wt(mes, i, t);
  98. e = d;
  99. d = c;
  100. c = rotl(b, 30);
  101. b = a;
  102. a = T;
  103. }
  104. a += a1;
  105. b += b1;
  106. c += c1;
  107. d += d1;
  108. e += e1;
  109. }
  110. return new uint[] { a, b, c, d, e };
  111. }
  112. }
  113. }

Решение задачи: «Не удается преобразовать из "string" в "Byte[]"»

textual
Листинг программы
  1. uint[] temp = new uint[] { a, b, c, d, e };
  2. byte[] hash = new byte[temp.Length*sizeof(uint)];
  3. Buffer.BlockCopy(temp, 0, hash, 0, hash.Length);
  4. for (int i=0, j=0; i<temp.Length; i++, j+=sizeof(uint))
  5. {
  6.     Array.Reverse(hash, j, sizeof(uint));
  7. }
  8. return hash;

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

14   голосов , оценка 4.143 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут