WmlComparerUtil.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright (c) Microsoft. All rights reserved.
  2. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
  3. using System;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. namespace OpenXmlPowerTools
  7. {
  8. internal static class WmlComparerUtil
  9. {
  10. public static string SHA1HashStringForUTF8String(string s)
  11. {
  12. var bytes = Encoding.UTF8.GetBytes(s);
  13. var sha1 = SHA1.Create();
  14. var hashBytes = sha1.ComputeHash(bytes);
  15. return HexStringFromBytes(hashBytes);
  16. }
  17. public static string SHA1HashStringForByteArray(byte[] bytes)
  18. {
  19. var sha1 = SHA1.Create();
  20. var hashBytes = sha1.ComputeHash(bytes);
  21. return HexStringFromBytes(hashBytes);
  22. }
  23. public static string HexStringFromBytes(byte[] bytes)
  24. {
  25. var sb = new StringBuilder();
  26. foreach (var b in bytes)
  27. {
  28. var hex = b.ToString("x2");
  29. sb.Append(hex);
  30. }
  31. return sb.ToString();
  32. }
  33. public static ComparisonUnitGroupType ComparisonUnitGroupTypeFromLocalName(string localName)
  34. {
  35. switch (localName)
  36. {
  37. case "p":
  38. return ComparisonUnitGroupType.Paragraph;
  39. case "tbl":
  40. return ComparisonUnitGroupType.Table;
  41. case "tr":
  42. return ComparisonUnitGroupType.Row;
  43. case "tc":
  44. return ComparisonUnitGroupType.Cell;
  45. case "txbxContent":
  46. return ComparisonUnitGroupType.Textbox;
  47. default:
  48. throw new ArgumentOutOfRangeException(nameof(localName),
  49. $@"Unsupported localName: '{localName}'.");
  50. }
  51. }
  52. }
  53. }