OpenSSLInterop.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. namespace WechatBakTool.Helpers
  6. {
  7. public class OpenSSLInterop
  8. {
  9. private const string Lib = "libcrypto-1_1";
  10. internal static unsafe int HMAC_Init(out HMAC_CTX ctx, byte[] key, int key_len, IntPtr md)
  11. {
  12. return HMAC_InitNative(out ctx, key, key_len, md);
  13. }
  14. internal static void HMAC_Init_ex(ref HMAC_CTX ctx, byte[] key, int key_len, IntPtr md, IntPtr zero)
  15. {
  16. HMAC_Init_exNative(ref ctx, key, key_len, md, zero);
  17. }
  18. internal static unsafe int HMAC_Update(ref HMAC_CTX ctx, byte* data, int len)
  19. {
  20. return HMAC_UpdateNative(ref ctx, data, len);
  21. }
  22. internal static unsafe int HMAC_Final(ref HMAC_CTX ctx, byte* md, ref uint len)
  23. {
  24. return HMAC_FinalNative(ref ctx, md, ref len);
  25. }
  26. [DllImport(Lib, CallingConvention = CallingConvention.Cdecl)]
  27. public extern static int PKCS5_PBKDF2_HMAC_SHA1(byte[] pass, int passlen, byte[] salt, int saltlen, int iter, int keylen, byte[] outBytes);
  28. [DllImport(Lib, EntryPoint = "HMAC_Init", ExactSpelling = true)]
  29. private extern static unsafe int HMAC_InitNative(out HMAC_CTX ctx, byte[] key, int key_len, IntPtr md);
  30. [DllImport(Lib, EntryPoint = "HMAC_Init_ex", ExactSpelling = true)]
  31. private extern static void HMAC_Init_exNative(ref HMAC_CTX ctx, byte[] key, int key_len, IntPtr md, IntPtr zero);
  32. [DllImport(Lib, EntryPoint = "HMAC_Update", ExactSpelling = true)]
  33. private extern static unsafe int HMAC_UpdateNative(ref HMAC_CTX ctx, byte* data, int len);
  34. [DllImport(Lib, EntryPoint = "HMAC_Final", ExactSpelling = true)]
  35. private extern static unsafe int HMAC_FinalNative(ref HMAC_CTX ctx, byte* md, ref uint len);
  36. [DllImport(Lib)]
  37. internal extern static unsafe void HMAC_CTX_cleanup(ref HMAC_CTX ctx);
  38. [StructLayout(LayoutKind.Explicit, Size = 512)]
  39. internal struct HMAC_CTX { }
  40. }
  41. }