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.1 kB

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