hashwrap.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // Code generator for hash wrappers
  3. //
  4. // Authors:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.IO;
  30. class Program {
  31. static string Template = @"//
  32. // NOTE: DO NOT EDIT - This file was automatically generated using
  33. // /mcs/class/System.Core/tools/hashwrap.cs
  34. //
  35. // System.Security.Cryptography.<className>
  36. //
  37. // Authors:
  38. // Sebastien Pouliot <[email protected]>
  39. //
  40. // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
  41. //
  42. // Permission is hereby granted, free of charge, to any person obtaining
  43. // a copy of this software and associated documentation files (the
  44. // 'Software'), to deal in the Software without restriction, including
  45. // without limitation the rights to use, copy, modify, merge, publish,
  46. // distribute, sublicense, and/or sell copies of the Software, and to
  47. // permit persons to whom the Software is furnished to do so, subject to
  48. // the following conditions:
  49. //
  50. // The above copyright notice and this permission notice shall be
  51. // included in all copies or substantial portions of the Software.
  52. //
  53. // THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  54. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  55. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  56. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  57. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  58. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  59. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  60. //
  61. namespace System.Security.Cryptography {
  62. // this is a wrapper around <wrapAround>
  63. // see README.CNG and README.CSP for more details
  64. public sealed class <className> : <baseName> {
  65. static byte[] Empty = new byte [0];
  66. private <baseName> hash;
  67. [SecurityCritical]
  68. public <className> ()
  69. {
  70. // note: we don't use <baseName>.Create since CryptoConfig could,
  71. // if set to use this class, result in a endless recursion
  72. hash = new <wrapAround> ();
  73. }
  74. [SecurityCritical]
  75. public override void Initialize ()
  76. {
  77. hash.Initialize ();
  78. }
  79. [SecurityCritical]
  80. protected override void HashCore (byte[] array, int ibStart, int cbSize)
  81. {
  82. hash.TransformBlock (array, ibStart, cbSize, null, 0);
  83. }
  84. [SecurityCritical]
  85. protected override byte[] HashFinal ()
  86. {
  87. hash.TransformFinalBlock (Empty, 0, 0);
  88. HashValue = hash.Hash;
  89. return HashValue;
  90. }
  91. [SecurityCritical]
  92. protected override void Dispose (bool disposing)
  93. {
  94. (hash as IDisposable).Dispose ();
  95. base.Dispose (disposing);
  96. }
  97. }
  98. }
  99. ";
  100. static int Main (string [] args)
  101. {
  102. int n = (args.Length / 3);
  103. if ((args.Length > 0) && ((args.Length % 3) != 0)) {
  104. Console.WriteLine ("usage: mono hashwrap.exe className baseName wrapAround [...]");
  105. return 1;
  106. }
  107. int p = 0;
  108. for (int i = 0; i < n; i++) {
  109. string className = args [p++]; // e.g. MD5Cng
  110. string baseName = args [p++]; // e.g. MD5
  111. string wrapAround = args [p++]; // e.g. MD5CryptoServiceProvider
  112. string filename = className + ".cs";
  113. using (StreamWriter writer = new StreamWriter (filename)) {
  114. string code = Template.Replace ("<className>", className);
  115. code = code.Replace ("<baseName>", baseName);
  116. code = code.Replace ("<wrapAround>", wrapAround);
  117. writer.Write (code);
  118. }
  119. }
  120. return 0;
  121. }
  122. }