private static byte[] Argon2_HashRaw(Argon2_HashRaw_Delegate algo, uint t_cost, uint m_cost, uint parallelism, byte[] password, byte[] salt, ulong hashlen = 64) { try { byte[] output = new byte[hashlen]; int r = algo(t_cost, m_cost, parallelism, password, (ulong)password.LongLength, salt, (ulong)(salt?.LongLength ?? 0), output, hashlen); if (r != 0) { return(null); } return(output); } catch { return(null); } }
/// <summary> /// Creates a new Argon2Sharp instance. <para> </para> /// Make sure to create one only once and cache it as needed, since loading the DLLs into memory could be, well, not so performant. /// <param name="sharedLibPathOverride">[OPTIONAL] Don't look for a <c>lib/</c> folder and directly use this path as a pre-resolved, platform-specific shared lib/DLL file path. Pass this if you want to handle the various platform's paths yourself.</param> /// </summary> public Argon2SharpContext(string sharedLibPathOverride = null) { string os; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { os = "windows"; loadUtils = new SharedLibLoadUtilsWindows(); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { os = "linux"; loadUtils = new SharedLibLoadUtilsLinux(); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { os = "mac"; loadUtils = new SharedLibLoadUtilsMac(); } else { throw new PlatformNotSupportedException("Unsupported OS"); } if (!string.IsNullOrEmpty(sharedLibPathOverride)) { LoadedLibraryPath = sharedLibPathOverride; } else { string cpu = RuntimeInformation.ProcessArchitecture switch { Architecture.X64 => "x64", Architecture.X86 => "x86", Architecture.Arm => "armeabi-v7a", Architecture.Arm64 => "arm64-v8a", _ => throw new PlatformNotSupportedException("CPU Architecture not supported!") }; string path = Path.Combine(Path.GetFullPath(Path.GetDirectoryName(Assembly.GetCallingAssembly().Location) ?? "."), "lib", cpu, os); if (!Directory.Exists(path)) { throw new PlatformNotSupportedException($"Shared library not found in {path} and/or unsupported CPU architecture. Please don't forget to copy the shared libraries/DLL into the 'lib/{{CPU_ARCHITECTURE}}/{{OS}}/{{SHARED_LIB_FILE}}' folder of your output build directory. "); } bool found = false; foreach (string file in Directory.GetFiles(path)) { if (file.ToLower().Contains("argon2")) { LoadedLibraryPath = Path.GetFullPath(Path.Combine(path, file)); found = true; break; } } if (!found) { throw new FileLoadException($"Shared library not found in {path} and/or unsupported CPU architecture. Please don't forget to copy the shared libraries/DLL into the 'lib/{{CPU_ARCHITECTURE}}/{{OS}}/{{SHARED_LIB_FILE}}' folder of your output build directory. "); } } lib = loadUtils.LoadLibrary(LoadedLibraryPath); if (lib == IntPtr.Zero) { goto hell; } IntPtr argon2i_hash_encoded = loadUtils.GetProcAddress(lib, "argon2i_hash_encoded"); if (argon2i_hash_encoded == IntPtr.Zero) { goto hell; } IntPtr argon2d_hash_encoded = loadUtils.GetProcAddress(lib, "argon2d_hash_encoded"); if (argon2d_hash_encoded == IntPtr.Zero) { goto hell; } IntPtr argon2id_hash_encoded = loadUtils.GetProcAddress(lib, "argon2id_hash_encoded"); if (argon2id_hash_encoded == IntPtr.Zero) { goto hell; } IntPtr argon2i_hash_raw = loadUtils.GetProcAddress(lib, "argon2i_hash_raw"); if (argon2i_hash_raw == IntPtr.Zero) { goto hell; } IntPtr argon2d_hash_raw = loadUtils.GetProcAddress(lib, "argon2d_hash_raw"); if (argon2d_hash_raw == IntPtr.Zero) { goto hell; } IntPtr argon2id_hash_raw = loadUtils.GetProcAddress(lib, "argon2id_hash_raw"); if (argon2id_hash_raw == IntPtr.Zero) { goto hell; } IntPtr argon2i_verify = loadUtils.GetProcAddress(lib, "argon2i_verify"); if (argon2i_verify == IntPtr.Zero) { goto hell; } IntPtr argon2d_verify = loadUtils.GetProcAddress(lib, "argon2d_verify"); if (argon2d_verify == IntPtr.Zero) { goto hell; } IntPtr argon2id_verify = loadUtils.GetProcAddress(lib, "argon2id_verify"); if (argon2id_verify == IntPtr.Zero) { goto hell; } argon2i_HashEncoded_Delegate = Marshal.GetDelegateForFunctionPointer <Argon2_HashEncoded_Delegate>(argon2i_hash_encoded); argon2d_HashEncoded_Delegate = Marshal.GetDelegateForFunctionPointer <Argon2_HashEncoded_Delegate>(argon2d_hash_encoded); argon2id_HashEncoded_Delegate = Marshal.GetDelegateForFunctionPointer <Argon2_HashEncoded_Delegate>(argon2id_hash_encoded); argon2i_HashRaw_Delegate = Marshal.GetDelegateForFunctionPointer <Argon2_HashRaw_Delegate>(argon2i_hash_raw); argon2d_HashRaw_Delegate = Marshal.GetDelegateForFunctionPointer <Argon2_HashRaw_Delegate>(argon2d_hash_raw); argon2id_HashRaw_Delegate = Marshal.GetDelegateForFunctionPointer <Argon2_HashRaw_Delegate>(argon2id_hash_raw); argon2i_Verify_Delegate = Marshal.GetDelegateForFunctionPointer <Argon2_Verify_Delegate>(argon2i_verify); argon2d_Verify_Delegate = Marshal.GetDelegateForFunctionPointer <Argon2_Verify_Delegate>(argon2d_verify); argon2id_Verify_Delegate = Marshal.GetDelegateForFunctionPointer <Argon2_Verify_Delegate>(argon2id_verify); return; hell: throw new Exception($"Failed to load one or more functions from the shared library \"{LoadedLibraryPath}\"!"); }