You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

MbedTLS.cs 2.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.IO;
  3. using System.Runtime.InteropServices;
  4. using Shadowsocks.Controller;
  5. using Shadowsocks.Properties;
  6. using Shadowsocks.Util;
  7. namespace Shadowsocks.Encryption
  8. {
  9. public class MbedTLS
  10. {
  11. const string DLLNAME = "libsscrypto";
  12. static MbedTLS()
  13. {
  14. string dllPath = Utils.GetTempPath("libsscrypto.dll");
  15. try
  16. {
  17. FileManager.UncompressFile(dllPath, Resources.libsscrypto_dll);
  18. }
  19. catch (IOException ex)
  20. {
  21. }
  22. catch (Exception e)
  23. {
  24. Console.WriteLine(e.ToString());
  25. }
  26. LoadLibrary(dllPath);
  27. }
  28. [DllImport("Kernel32.dll")]
  29. private static extern IntPtr LoadLibrary(string path);
  30. public const int MD5_CTX_SIZE = 88;
  31. public static byte[] MD5(byte[] input)
  32. {
  33. IntPtr ctx = Marshal.AllocHGlobal(MD5_CTX_SIZE);
  34. byte[] output = new byte[16];
  35. MbedTLS.md5_init(ctx);
  36. MbedTLS.md5_starts(ctx);
  37. MbedTLS.md5_update(ctx, input, (uint)input.Length);
  38. MbedTLS.md5_finish(ctx, output);
  39. MbedTLS.md5_free(ctx);
  40. Marshal.FreeHGlobal(ctx);
  41. return output;
  42. }
  43. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  44. public extern static void md5_init(IntPtr ctx);
  45. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  46. public extern static void md5_free(IntPtr ctx);
  47. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  48. public extern static void md5_starts(IntPtr ctx);
  49. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  50. public extern static void md5_update(IntPtr ctx, byte[] input, uint ilen);
  51. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  52. public extern static void md5_finish(IntPtr ctx, byte[] output);
  53. }
  54. }