CryptoUtil.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*===============================================================================
  2. Copyright (C) 2022 Immersal - Part of Hexagon. All Rights Reserved.
  3. This file is part of the Immersal SDK.
  4. The Immersal SDK cannot be copied, distributed, or made available to
  5. third-parties for commercial purposes without written permission of Immersal Ltd.
  6. Contact sdk@immersal.com for licensing requests.
  7. ===============================================================================*/
  8. using System;
  9. using System.Security.Cryptography;
  10. namespace Immersal.Samples.Util
  11. {
  12. public static class CryptoUtil
  13. {
  14. public static string MD5(byte[] bytes)
  15. {
  16. MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  17. byte[] hashBytes = md5.ComputeHash(bytes);
  18. string hashString = "";
  19. for (int i = 0; i < hashBytes.Length; i++)
  20. hashString += Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
  21. return hashString.PadLeft(32, '0');
  22. }
  23. public static string SHA256(byte[] bytes)
  24. {
  25. SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider();
  26. byte[] hashBytes = sha256.ComputeHash(bytes);
  27. string hashString = "";
  28. for (int i = 0; i < hashBytes.Length; i++)
  29. hashString += Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
  30. return hashString.PadLeft(64, '0');
  31. }
  32. }
  33. }