|
| 1 | +using System.Runtime.InteropServices; |
| 2 | +using System.Text; |
| 3 | +using System.Text.Json; |
| 4 | + |
| 5 | +namespace Bit.RustSDK; |
| 6 | + |
| 7 | +public class UserKeys |
| 8 | +{ |
| 9 | + public required string MasterPasswordHash { get; set; } |
| 10 | + /// <summary> |
| 11 | + /// Base64 encoded UserKey |
| 12 | + /// </summary> |
| 13 | + public required string Key { get; set; } |
| 14 | + public required string EncryptedUserKey { get; set; } |
| 15 | + public required string PublicKey { get; set; } |
| 16 | + public required string PrivateKey { get; set; } |
| 17 | +} |
| 18 | + |
| 19 | +public class OrganizationKeys |
| 20 | +{ |
| 21 | + /// <summary> |
| 22 | + /// Base64 encoded SymmetricCryptoKey |
| 23 | + /// </summary> |
| 24 | + public required string Key { get; set; } |
| 25 | + |
| 26 | + public required string PublicKey { get; set; } |
| 27 | + public required string PrivateKey { get; set; } |
| 28 | +} |
| 29 | + |
| 30 | +/// <summary> |
| 31 | +/// Service implementation that provides a C# friendly interface to the Rust SDK |
| 32 | +/// </summary> |
| 33 | +public class RustSdkService |
| 34 | +{ |
| 35 | + private static readonly JsonSerializerOptions CaseInsensitiveOptions = new() |
| 36 | + { |
| 37 | + PropertyNameCaseInsensitive = true |
| 38 | + }; |
| 39 | + |
| 40 | + public unsafe UserKeys GenerateUserKeys(string email, string password) |
| 41 | + { |
| 42 | + var emailBytes = StringToRustString(email); |
| 43 | + var passwordBytes = StringToRustString(password); |
| 44 | + |
| 45 | + fixed (byte* emailPtr = emailBytes) |
| 46 | + fixed (byte* passwordPtr = passwordBytes) |
| 47 | + { |
| 48 | + var resultPtr = NativeMethods.generate_user_keys(emailPtr, passwordPtr); |
| 49 | + |
| 50 | + var result = TakeAndDestroyRustString(resultPtr); |
| 51 | + |
| 52 | + return JsonSerializer.Deserialize<UserKeys>(result, CaseInsensitiveOptions)!; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public unsafe OrganizationKeys GenerateOrganizationKeys() |
| 57 | + { |
| 58 | + var resultPtr = NativeMethods.generate_organization_keys(); |
| 59 | + |
| 60 | + var result = TakeAndDestroyRustString(resultPtr); |
| 61 | + |
| 62 | + return JsonSerializer.Deserialize<OrganizationKeys>(result, CaseInsensitiveOptions)!; |
| 63 | + } |
| 64 | + |
| 65 | + public unsafe string GenerateUserOrganizationKey(string userKey, string orgKey) |
| 66 | + { |
| 67 | + var userKeyBytes = StringToRustString(userKey); |
| 68 | + var orgKeyBytes = StringToRustString(orgKey); |
| 69 | + |
| 70 | + fixed (byte* userKeyPtr = userKeyBytes) |
| 71 | + fixed (byte* orgKeyPtr = orgKeyBytes) |
| 72 | + { |
| 73 | + var resultPtr = NativeMethods.generate_user_organization_key(userKeyPtr, orgKeyPtr); |
| 74 | + |
| 75 | + var result = TakeAndDestroyRustString(resultPtr); |
| 76 | + |
| 77 | + return result; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + private static byte[] StringToRustString(string str) |
| 83 | + { |
| 84 | + return Encoding.UTF8.GetBytes(str + '\0'); |
| 85 | + } |
| 86 | + |
| 87 | + private static unsafe string TakeAndDestroyRustString(byte* ptr) |
| 88 | + { |
| 89 | + if (ptr == null) |
| 90 | + { |
| 91 | + throw new RustSdkException("Pointer is null"); |
| 92 | + } |
| 93 | + |
| 94 | + var result = Marshal.PtrToStringUTF8((IntPtr)ptr); |
| 95 | + NativeMethods.free_c_string(ptr); |
| 96 | + |
| 97 | + if (result == null) |
| 98 | + { |
| 99 | + throw new RustSdkException("Failed to convert native result to string"); |
| 100 | + } |
| 101 | + |
| 102 | + return result; |
| 103 | + } |
| 104 | +} |
0 commit comments