2 // CommonCrypto code generator for digest algorithms
5 // Sebastien Pouliot <sebastien@xamarin.com>
7 // Copyright 2012-2014 Xamarin Inc.
15 public static class CommonDigest {
17 #if !MONOTOUCH && !XAMMAC
18 // we do not add anything in MonoTouch, just replacing, so this is not needed
19 // however we can avoid a dependency on Mono.Security for Crimson.CommonCrypto.dll by including the base classes
20 static public void GenerateBaseClass (string namespaceName, string typeName, string baseTypeName, int hashSize,
21 string visibilityStart = "", string visibilityEnd = "")
23 string template = @"// Generated file to bind CommonCrypto/CommonDigest - DO NOT EDIT
26 // Sebastien Pouliot <sebastien@xamarin.com>
28 // Copyright 2012-2014 Xamarin Inc.
31 using System.Security.Cryptography;
32 using System.Runtime.InteropServices;
34 namespace %NAMESPACE% {
39 abstract class %BASE% : HashAlgorithm {
43 HashSizeValue = %HASHSIZE%;
46 public static new %BASE% Create ()
48 return Create (""%BASE%"");
51 public static new %BASE% Create (string hashName)
53 object o = CryptoConfig.CreateFromName (hashName);
54 return (%BASE%) o ?? new %TYPE% ();
59 File.WriteAllText (baseTypeName + ".g.cs", template.Replace ("%NAMESPACE%", namespaceName).
60 Replace ("%TYPE%", typeName).Replace ("%BASE%", baseTypeName).
61 Replace ("%VISIBILITY_START%", visibilityStart).Replace ("%VISIBILITY_END%", visibilityEnd).
62 Replace ("%HASHSIZE%", hashSize.ToString ()));
65 static public void Generate (string namespaceName, string typeName, string baseTypeName, int contextSize,
66 string visibilityStart = "", string visibilityEnd = "")
68 string template = @"// Generated file to bind CommonCrypto/CommonDigest - DO NOT EDIT
71 // Sebastien Pouliot <sebastien@xamarin.com>
73 // Copyright 2011-2014 Xamarin Inc.
76 using System.Security.Cryptography;
77 using System.Runtime.InteropServices;
79 using Mono.Security.Cryptography;
81 namespace %NAMESPACE% {
86 sealed class %TYPE% : %BASE% {
90 [DllImport (""/usr/lib/libSystem.dylib"", EntryPoint=""CC_%BASE%_Init"")]
91 static extern int Init (/* CC_%BASE%_CTX */ IntPtr c);
93 [DllImport (""/usr/lib/libSystem.dylib"", EntryPoint=""CC_%BASE%_Update"")]
94 static extern int Update (/* CC_%BASE%_CTX */ IntPtr c, /* const void * */ IntPtr data, /* uint32_t */ uint len);
96 [DllImport (""/usr/lib/libSystem.dylib"", EntryPoint=""CC_%BASE%_Final"")]
97 static extern int Final (/* unsigned char * */ byte [] md, /* CC_%BASE%_CTX */ IntPtr c);
109 protected override void Dispose (bool disposing)
111 if (ctx != IntPtr.Zero) {
112 Marshal.FreeHGlobal (ctx);
115 base.Dispose (disposing);
116 GC.SuppressFinalize (this);
119 public override void Initialize ()
121 if (ctx == IntPtr.Zero)
122 ctx = Marshal.AllocHGlobal (%CTX_SIZE%);
126 throw new CryptographicException (hr);
129 protected override void HashCore (byte[] data, int start, int length)
131 if (ctx == IntPtr.Zero)
134 if (data.Length == 0)
138 fixed (byte* p = &data [0]) {
139 int hr = Update (ctx, (IntPtr) (p + start), (uint) length);
141 throw new CryptographicException (hr);
146 protected override byte[] HashFinal ()
148 if (ctx == IntPtr.Zero)
151 byte[] data = new byte [HashSize >> 3];
152 int hr = Final (data, ctx);
154 throw new CryptographicException (hr);
161 File.WriteAllText (typeName + ".g.cs", template.Replace ("%NAMESPACE%", namespaceName).
162 Replace ("%TYPE%", typeName).Replace ("%BASE%", baseTypeName).
163 Replace ("%VISIBILITY_START%", visibilityStart).Replace ("%VISIBILITY_END%", visibilityEnd).
164 Replace ("%CTX_SIZE%", contextSize.ToString ()));