static private void GenerateMHash(string dir, MHashId id) { string algo = id.ToString().ToUpper(); double fx = 1.0; switch (algo) { case "SHA1": case "MD5": case "SHA256": case "SHA384": case "SHA512": // base class already defined in Fx 1.0 break; case "RIPEMD160": // base class already defined, but only in Fx 2.0 GenerateMHashAbstractClass(dir, id, 2.0); break; default: // we need to generate our own base class GenerateMHashAbstractClass(dir, id, 1.0); break; } GenerateMHashClass(dir, id, fx); }
static private void GenerateMHashClass(string dir, MHashId id, double fx) { string algo = id.ToString().ToUpper(); string classname = String.Format("{0}Native", algo); string baseclass = " : " + algo; StringBuilder sb = new StringBuilder(header); sb.Append(mhash_start); // if (fx < 2.0){ sb.AppendFormat("{0}\tpublic class {1}{2} {{{0}", Environment.NewLine, classname, baseclass); // } else { // sb.AppendFormat ("{0}#if NET_2_0{0}\tpublic class {1}{2} {{{0}#else{0}\tpublic class {1}{2 : HashAlgorithm {{{0}#endif{0}", // Environment.NewLine, classname, baseclass); // } sb.AppendFormat("{0}\t\tprivate MHashId Id = MHashId.{1};{0}", Environment.NewLine, id); sb.Append("\t\tprivate MHashHelper hash;"); sb.AppendFormat("{0}{0}\t\tpublic {1} ()", Environment.NewLine, classname); sb.Append(mhash_ctor); sb.AppendFormat("\t\t~{0} ()", classname); sb.Append(mhash_end); sb.Append(footer); string filename = Path.Combine(dir, classname + ".cs"); WriteToFile(filename, sb.ToString()); }
static private void GenerateMHashAbstractClass(string dir, MHashId id, double fx) { string algo = id.ToString().ToUpper(); int size = ((int)MHashWrapper.mhash_get_block_size(id) << 3); // bytes to bits StringBuilder sb = new StringBuilder(header); sb.Append(mhash_base_start); sb.AppendFormat("{0}\tpublic abstract class {1} : HashAlgorithm {{{0}", Environment.NewLine, algo); sb.AppendFormat("{0}\t\tprotected {1} (){0}\t\t{{", Environment.NewLine, algo); sb.AppendFormat("{0}\t\t\t// {1} digest length is {2} bits long", Environment.NewLine, algo, size); sb.AppendFormat("{0}\t\t\tHashSizeValue = {1};{0}\t\t}}{0}", Environment.NewLine, size); sb.AppendFormat("{0}\t\tpublic static new {1} Create (){0}\t\t{{", Environment.NewLine, algo); sb.AppendFormat("{0}\t\t\treturn Create (\"{1}\");{0}\t\t}}{0}", Environment.NewLine, algo); sb.AppendFormat("{0}\t\tpublic static new {1} Create (string hashName){0}\t\t{{{0}", Environment.NewLine, algo); sb.Append("\t\t\tobject o = CryptoConfig.CreateFromName (hashName);"); sb.AppendFormat("{0}\t\t\t// in case machine.config isn't configured to use any {1} implementation", Environment.NewLine, algo); sb.AppendFormat("{0}\t\t\tif (o == null) {{{0}\t\t\t\to = new {1}Native ();{0}\t\t\t}}", Environment.NewLine, algo); sb.AppendFormat("{0}\t\t\treturn ({1}) o;{0}\t\t}}{0}\t}}{0}}}", Environment.NewLine, algo); sb.Append(footer); string filename = Path.Combine(dir, algo + ".cs"); WriteToFile(filename, sb.ToString()); }
public MHashHelper(MHashId type) { this.type = type; handle = MHashWrapper.mhash_init (type); if (handle == IntPtr.Zero) { string msg = String.Format ("Unknown mhash id '{0}'.", type); throw new CryptographicException (msg); } blocksize = (int) MHashWrapper.mhash_get_block_size (type); }
public MHashHelper(MHashId type) { this.type = type; handle = MHashWrapper.mhash_init(type); if (handle == IntPtr.Zero) { string msg = String.Format("Unknown mhash id '{0}'.", type); throw new CryptographicException(msg); } blocksize = (int)MHashWrapper.mhash_get_block_size(type); }
internal static extern IntPtr mhash_init(MHashId type);
internal static extern IntPtr mhash_get_block_size(MHashId type);
private static void GenerateMHashClass(string dir, MHashId id, double fx) { string algo = id.ToString ().ToUpper (); string classname = String.Format ("{0}Native", algo); string baseclass = " : " + algo; StringBuilder sb = new StringBuilder (header); sb.Append (mhash_start); // if (fx < 2.0){ sb.AppendFormat ("{0}\tpublic class {1}{2} {{{0}", Environment.NewLine, classname, baseclass); // } else { // sb.AppendFormat ("{0}#if NET_2_0{0}\tpublic class {1}{2} {{{0}#else{0}\tpublic class {1}{2 : HashAlgorithm {{{0}#endif{0}", // Environment.NewLine, classname, baseclass); // } sb.AppendFormat ("{0}\t\tprivate MHashId Id = MHashId.{1};{0}", Environment.NewLine, id); sb.Append ("\t\tprivate MHashHelper hash;"); sb.AppendFormat ("{0}{0}\t\tpublic {1} ()", Environment.NewLine, classname); sb.Append (mhash_ctor); sb.AppendFormat ("\t\t~{0} ()", classname); sb.Append (mhash_end); sb.Append (footer); string filename = Path.Combine (dir, classname + ".cs"); WriteToFile (filename, sb.ToString ()); }
private static void GenerateMHashAbstractClass(string dir, MHashId id, double fx) { string algo = id.ToString ().ToUpper (); int size = ((int) MHashWrapper.mhash_get_block_size (id) << 3); // bytes to bits StringBuilder sb = new StringBuilder (header); sb.Append (mhash_base_start); sb.AppendFormat ("{0}\tpublic abstract class {1} : HashAlgorithm {{{0}", Environment.NewLine, algo); sb.AppendFormat ("{0}\t\tprotected {1} (){0}\t\t{{", Environment.NewLine, algo); sb.AppendFormat ("{0}\t\t\t// {1} digest length is {2} bits long", Environment.NewLine, algo, size); sb.AppendFormat ("{0}\t\t\tHashSizeValue = {1};{0}\t\t}}{0}", Environment.NewLine, size); sb.AppendFormat ("{0}\t\tpublic static new {1} Create (){0}\t\t{{", Environment.NewLine, algo); sb.AppendFormat ("{0}\t\t\treturn Create (\"{1}\");{0}\t\t}}{0}", Environment.NewLine, algo); sb.AppendFormat ("{0}\t\tpublic static new {1} Create (string hashName){0}\t\t{{{0}", Environment.NewLine, algo); sb.Append ("\t\t\tobject o = CryptoConfig.CreateFromName (hashName);"); sb.AppendFormat ("{0}\t\t\t// in case machine.config isn't configured to use any {1} implementation", Environment.NewLine, algo); sb.AppendFormat ("{0}\t\t\tif (o == null) {{{0}\t\t\t\to = new {1}Native ();{0}\t\t\t}}", Environment.NewLine, algo); sb.AppendFormat ("{0}\t\t\treturn ({1}) o;{0}\t\t}}{0}\t}}{0}}}", Environment.NewLine, algo); sb.Append (footer); string filename = Path.Combine (dir, algo + ".cs"); WriteToFile (filename, sb.ToString ()); }
private static void GenerateMHash(string dir, MHashId id) { string algo = id.ToString ().ToUpper (); double fx = 1.0; switch (algo) { case "SHA1": case "MD5": case "SHA256": case "SHA384": case "SHA512": // base class already defined in Fx 1.0 break; case "RIPEMD160": // base class already defined, but only in Fx 2.0 GenerateMHashAbstractClass (dir, id, 2.0); break; default: // we need to generate our own base class GenerateMHashAbstractClass (dir, id, 1.0); break; } GenerateMHashClass (dir, id, fx); }
internal extern static IntPtr mhash_get_block_size(MHashId type);
internal extern static IntPtr mhash_init(MHashId type);