CapyKit/Docs/_site/api/CapyKit.Helpers.SecurityHelper.html

302 lines
20 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> Class SecurityHelper | CapyKit Documentation </title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="title" content=" Class SecurityHelper | CapyKit Documentation ">
<link rel="icon" href="../favicon.ico">
<link rel="stylesheet" href="../public/docfx.min.css">
<link rel="stylesheet" href="../public/main.css">
<meta name="docfx:navrel" content="../toc.html">
<meta name="docfx:tocrel" content="../toc.html">
<meta name="docfx:rel" content="../">
<meta name="loc:inThisArticle" content="In this article">
<meta name="loc:searchResultsCount" content="{count} results for &quot;{query}&quot;">
<meta name="loc:searchNoResults" content="No results for &quot;{query}&quot;">
<meta name="loc:tocFilter" content="Filter by title">
<meta name="loc:nextArticle" content="Next">
<meta name="loc:prevArticle" content="Previous">
<meta name="loc:themeLight" content="Light">
<meta name="loc:themeDark" content="Dark">
<meta name="loc:themeAuto" content="Auto">
<meta name="loc:changeTheme" content="Change theme">
<meta name="loc:copy" content="Copy">
<meta name="loc:downloadPdf" content="Download PDF">
<script type="module" src="./../public/docfx.min.js"></script>
<script>
const theme = localStorage.getItem('theme') || 'auto'
document.documentElement.setAttribute('data-bs-theme', theme === 'auto' ? (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light') : theme)
</script>
</head>
<body class="tex2jax_ignore" data-layout="" data-yaml-mime="">
<header class="bg-body border-bottom">
<nav id="autocollapse" class="navbar navbar-expand-md" role="navigation">
<div class="container-xxl flex-nowrap">
<a class="navbar-brand" href="../index.html">
<img id="logo" class="svg" src="../logo.svg" alt="CapyKit Documentation">
CapyKit Documentation
</a>
<button class="btn btn-lg d-md-none border-0" type="button" data-bs-toggle="collapse" data-bs-target="#navpanel" aria-controls="navpanel" aria-expanded="false" aria-label="Toggle navigation">
<i class="bi bi-three-dots"></i>
</button>
<div class="collapse navbar-collapse" id="navpanel">
<div id="navbar">
<form class="search" role="search" id="search">
<i class="bi bi-search"></i>
<input class="form-control" id="search-query" type="search" disabled placeholder="Search" autocomplete="off" aria-label="Search">
</form>
</div>
</div>
</div>
</nav>
</header>
<main class="container-xxl">
<div class="content">
<div class="actionbar">
<nav id="breadcrumb"></nav>
</div>
<article data-uid="">
<h1 id="CapyKit_Helpers_SecurityHelper"> Class SecurityHelper</h1>
<p>Namespace: <a href="CapyKit.Helpers.html">CapyKit.Helpers</a><br>
Assembly: CapyKit.dll</p>
<p>A class that contains methods for managing secure data processing and cryptography.</p>
<pre><code class="lang-csharp">public class SecurityHelper
</code></pre>
<h4 id="inheritance">Inheritance</h4>
<p><a href="https://learn.microsoft.com/dotnet/api/system.object">object</a>
<a href="CapyKit.Helpers.SecurityHelper.html">SecurityHelper</a></p>
<h4 id="inherited-members">Inherited Members</h4>
<p><a href="https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object)">object.Equals(object?)</a>,
<a href="https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object-system-object)">object.Equals(object?, object?)</a>,
<a href="https://learn.microsoft.com/dotnet/api/system.object.gethashcode">object.GetHashCode()</a>,
<a href="https://learn.microsoft.com/dotnet/api/system.object.gettype">object.GetType()</a>,
<a href="https://learn.microsoft.com/dotnet/api/system.object.memberwiseclone">object.MemberwiseClone()</a>,
<a href="https://learn.microsoft.com/dotnet/api/system.object.referenceequals">object.ReferenceEquals(object?, object?)</a>,
<a href="https://learn.microsoft.com/dotnet/api/system.object.tostring">object.ToString()</a></p>
<h4 id="extension-methods">Extension Methods</h4>
<p><a href="CapyKit.Extensions.ObjectExtensions.html#CapyKit_Extensions_ObjectExtensions_UpdateProperties__1___0___0_">ObjectExtensions.UpdateProperties&lt;SecurityHelper&gt;(SecurityHelper, SecurityHelper)</a>,
<a href="CapyKit.Extensions.ObjectExtensions.html#CapyKit_Extensions_ObjectExtensions_UpdateProperties_System_Object_System_Object_">ObjectExtensions.UpdateProperties(object, object)</a></p>
<h2 id="methods">Methods</h2>
<h3 id="CapyKit_Helpers_SecurityHelper_CompareHashedPassword__1_CapyKit_Password_System_String_System_Byte___System_Object___"> CompareHashedPassword&lt;T&gt;(Password, string, byte[], params object[])</h3>
<p>Compares an unencrypted <code class="paramref">password</code> with a stored, encrypted <code class="paramref">existingPassword</code>.
This method uses the specified password algorithm type <code class="typeparamref">T</code> to retrieve the hashed version
of the <code class="paramref">password</code> and then compares it with the <code class="paramref">existingPassword</code>.</p>
<pre><code class="lang-csharp">public static bool CompareHashedPassword&lt;T&gt;(Password existingPassword, string password, byte[] salt, params object[] args)
</code></pre>
<h4 id="parameters">Parameters</h4>
<p><code>existingPassword</code> <a href="CapyKit.Password.html">Password</a></p>
<p>The existing, encrypted password.</p>
<p><code>password</code> <a href="https://learn.microsoft.com/dotnet/api/system.string">string</a></p>
<p>The unencrypted password to be compared.</p>
<p><code>salt</code> <a href="https://learn.microsoft.com/dotnet/api/system.byte">byte</a>[]</p>
<p>The salt value used in password hashing.</p>
<p><code>args</code> <a href="https://learn.microsoft.com/dotnet/api/system.object">object</a>[]</p>
<p>Additional arguments required for constructing the password algorithm instance.</p>
<h4 id="returns">Returns</h4>
<p><a href="https://learn.microsoft.com/dotnet/api/system.boolean">bool</a></p>
<p><a href="https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool">true</a> if hash comparison succeeds, <a href="https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool">false</a> if it fails.</p>
<h4 id="type-parameters">Type Parameters</h4>
<p><code>T</code></p>
<p>The type of the password hashing algorithm.</p>
<h3 id="CapyKit_Helpers_SecurityHelper_CompareHashedPassword_CapyKit_Password_System_String_System_Byte___CapyKit_IPasswordAlgorithm_System_Object___"> CompareHashedPassword(Password, string, byte[], IPasswordAlgorithm, params object[])</h3>
<p>Compares an unencrypted <code class="paramref">password</code> with a stored, encrypted <code class="paramref">existingPassword</code>.
This method uses the specified <code class="paramref">algorithm</code> to retrieve the hashed version
of the <code class="paramref">password</code> and then compares it with the <code class="paramref">existingPassword</code>.</p>
<pre><code class="lang-csharp">public static bool CompareHashedPassword(Password existingPassword, string password, byte[] salt, IPasswordAlgorithm algorithm, params object[] args)
</code></pre>
<h4 id="parameters-1">Parameters</h4>
<p><code>existingPassword</code> <a href="CapyKit.Password.html">Password</a></p>
<p>The existing, encrypted password.</p>
<p><code>password</code> <a href="https://learn.microsoft.com/dotnet/api/system.string">string</a></p>
<p>The unencrypted password to be compared.</p>
<p><code>salt</code> <a href="https://learn.microsoft.com/dotnet/api/system.byte">byte</a>[]</p>
<p>The salt value used in password hashing.</p>
<p><code>algorithm</code> <a href="CapyKit.IPasswordAlgorithm.html">IPasswordAlgorithm</a></p>
<p>The password hashing algorithm.</p>
<p><code>args</code> <a href="https://learn.microsoft.com/dotnet/api/system.object">object</a>[]</p>
<p>Additional arguments required for constructing the password algorithm instance.</p>
<h4 id="returns-1">Returns</h4>
<p><a href="https://learn.microsoft.com/dotnet/api/system.boolean">bool</a></p>
<p><a href="https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool">true</a> if hash comparison succeeds, <a href="https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool">false</a> if it fails.</p>
<h3 id="CapyKit_Helpers_SecurityHelper_CompareSessionID_System_String_System_String_"> CompareSessionID(string, string)</h3>
<p>Compares two session identifiers.</p>
<pre><code class="lang-csharp">public static bool CompareSessionID(string first, string second)
</code></pre>
<h4 id="parameters-2">Parameters</h4>
<p><code>first</code> <a href="https://learn.microsoft.com/dotnet/api/system.string">string</a></p>
<p>The first session identifier.</p>
<p><code>second</code> <a href="https://learn.microsoft.com/dotnet/api/system.string">string</a></p>
<p>The second session identifier.</p>
<h4 id="returns-2">Returns</h4>
<p><a href="https://learn.microsoft.com/dotnet/api/system.boolean">bool</a></p>
<p><a href="https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool">true</a> if comparison succeeds, <a href="https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool">false</a> if not.</p>
<h3 id="CapyKit_Helpers_SecurityHelper_GetPassword__1_System_String_System_Object___"> GetPassword&lt;T&gt;(string, params object[])</h3>
<p>Retrieves a <span class="xref">CapyKit.Password</span> object using the specified password and generates a random salt value.
Then it uses that salt to call the overloaded <span class="xref">CapyKit.Helpers.SecurityHelper.GetPassword``1(System.String,System.Byte[],System.Object[])</span> method with the given password and
the generated salt as arguments.</p>
<pre><code class="lang-csharp">public static Password GetPassword&lt;T&gt;(string password, params object[] args)
</code></pre>
<h4 id="parameters-3">Parameters</h4>
<p><code>password</code> <a href="https://learn.microsoft.com/dotnet/api/system.string">string</a></p>
<p>The plaintext password to be hashed.</p>
<p><code>args</code> <a href="https://learn.microsoft.com/dotnet/api/system.object">object</a>[]</p>
<p>Optional constructor arguments for the <span class="xref">CapyKit.IPasswordAlgorithm</span> implementation
instance.</p>
<h4 id="returns-3">Returns</h4>
<p><a href="CapyKit.Password.html">Password</a></p>
<p>A new <span class="xref">CapyKit.Password</span> object with the given password and a randomly generated salt, as well as an
instance of <code class="typeparamref">T</code> created using any optional constructor arguments provided.</p>
<h4 id="type-parameters-1">Type Parameters</h4>
<p><code>T</code></p>
<p>The type of <span class="xref">CapyKit.IPasswordAlgorithm</span> implementation to use.</p>
<h4 id="see-also">See Also</h4>
<p><a href="CapyKit.Helpers.SecurityHelper.html">SecurityHelper</a>.SALT_SIZE</p>
<h3 id="CapyKit_Helpers_SecurityHelper_GetPassword__1_System_String_System_Byte___System_Object___"> GetPassword&lt;T&gt;(string, byte[], params object[])</h3>
<p>Retrieves a <span class="xref">CapyKit.Password</span> object using the specified password, salt, and optional
constructor arguments.</p>
<pre><code class="lang-csharp">public static Password GetPassword&lt;T&gt;(string password, byte[] salt, params object[] args) where T : IPasswordAlgorithm
</code></pre>
<h4 id="parameters-4">Parameters</h4>
<p><code>password</code> <a href="https://learn.microsoft.com/dotnet/api/system.string">string</a></p>
<p>The plaintext password to be hashed.</p>
<p><code>salt</code> <a href="https://learn.microsoft.com/dotnet/api/system.byte">byte</a>[]</p>
<p>A random value used as an additional input to the one-way function that hashes data, a
password or passphrase. This is used to make each output different for the same input
thus adding security.</p>
<p><code>args</code> <a href="https://learn.microsoft.com/dotnet/api/system.object">object</a>[]</p>
<p>Optional constructor arguments for the <span class="xref">CapyKit.IPasswordAlgorithm</span> implementation
instance.</p>
<h4 id="returns-4">Returns</h4>
<p><a href="CapyKit.Password.html">Password</a></p>
<p>A new <span class="xref">CapyKit.Password</span> object with the given password and salt, as well as an instance
of <code class="typeparamref">T</code> created using the provided constructor arguments.</p>
<h4 id="type-parameters-2">Type Parameters</h4>
<p><code>T</code></p>
<p>The type of <span class="xref">CapyKit.IPasswordAlgorithm</span> implementation to use.</p>
<h4 id="remarks">Remarks</h4>
<p>This method uses reflection to find a constructor for the specified password algorithm type (<code class="typeparamref">T</code>).
It emits an error event if a suitable constructor is not found or if there is an error invoking the constructor.</p>
<h3 id="CapyKit_Helpers_SecurityHelper_GetRandomPassword_System_Int32_CapyKit_Helpers_ValidCharacterCollection___"> GetRandomPassword(int, params ValidCharacterCollection[])</h3>
<p>Gets a cryptographically strong random password.</p>
<pre><code class="lang-csharp">public static string GetRandomPassword(int length, params ValidCharacterCollection[] validCharacters)
</code></pre>
<h4 id="parameters-5">Parameters</h4>
<p><code>length</code> <a href="https://learn.microsoft.com/dotnet/api/system.int32">int</a></p>
<p>The length of the password to generate.</p>
<p><code>validCharacters</code> <a href="CapyKit.Helpers.ValidCharacterCollection.html">ValidCharacterCollection</a>[]</p>
<p>An array of <span class="xref">CapyKit.Helpers.ValidCharacterCollection</span> enumeration values representing the desired
character sets.</p>
<h4 id="returns-5">Returns</h4>
<p><a href="https://learn.microsoft.com/dotnet/api/system.string">string</a></p>
<p>The password.</p>
<h3 id="CapyKit_Helpers_SecurityHelper_GetRandomString_System_Int32_"> GetRandomString(int)</h3>
<p>A convenience method to generate a random string of the specified length using all character sets.</p>
<pre><code class="lang-csharp">public static string GetRandomString(int length)
</code></pre>
<h4 id="parameters-6">Parameters</h4>
<p><code>length</code> <a href="https://learn.microsoft.com/dotnet/api/system.int32">int</a></p>
<p>The desired length of the generated random string.</p>
<h4 id="returns-6">Returns</h4>
<p><a href="https://learn.microsoft.com/dotnet/api/system.string">string</a></p>
<h4 id="see-also-1">See Also</h4>
<p><a href="CapyKit.Helpers.ValidCharacterCollection.html">ValidCharacterCollection</a>,
<a href="CapyKit.Helpers.SecurityHelper.html">SecurityHelper</a>.<a href="CapyKit.Helpers.SecurityHelper.html#CapyKit_Helpers_SecurityHelper_GetRandomString_System_Int32_CapyKit_Helpers_ValidCharacterCollection___">GetRandomString</a>(<a href="https://learn.microsoft.com/dotnet/api/system.int32">int</a>, params <a href="CapyKit.Helpers.ValidCharacterCollection.html">ValidCharacterCollection</a>[])</p>
<h3 id="CapyKit_Helpers_SecurityHelper_GetRandomString_System_Int32_CapyKit_Helpers_ValidCharacterCollection___"> GetRandomString(int, params ValidCharacterCollection[])</h3>
<p>Gets a cryptographically strong random string using the character values found in VALID_CHARACTERS.</p>
<pre><code class="lang-csharp">public static string GetRandomString(int length, params ValidCharacterCollection[] validChars)
</code></pre>
<h4 id="parameters-7">Parameters</h4>
<p><code>length</code> <a href="https://learn.microsoft.com/dotnet/api/system.int32">int</a></p>
<p>The length of the string to create.</p>
<p><code>validChars</code> <a href="CapyKit.Helpers.ValidCharacterCollection.html">ValidCharacterCollection</a>[]</p>
<h4 id="returns-7">Returns</h4>
<p><a href="https://learn.microsoft.com/dotnet/api/system.string">string</a></p>
<p>The random string.</p>
<h3 id="CapyKit_Helpers_SecurityHelper_GetSalt_System_Int32_"> GetSalt(int)</h3>
<p>Generates a random byte array that can act as a salt.</p>
<pre><code class="lang-csharp">public static byte[] GetSalt(int length = 32)
</code></pre>
<h4 id="parameters-8">Parameters</h4>
<p><code>length</code> <a href="https://learn.microsoft.com/dotnet/api/system.int32">int</a></p>
<p>(Optional) The desired length of the generated byte array.</p>
<h4 id="returns-8">Returns</h4>
<p><a href="https://learn.microsoft.com/dotnet/api/system.byte">byte</a>[]</p>
<p>An array of byte.</p>
<h4 id="remarks-1">Remarks</h4>
<p>A default length of <span class="xref">CapyKit.Helpers.SecurityHelper.SALT_SIZE</span> is provided as a sane default. Larger values can be used for increased
entropy.</p>
<h3 id="CapyKit_Helpers_SecurityHelper_Pbkdf2_System_String_System_Byte___"> Pbkdf2(string, byte[])</h3>
<p>Generates a new <span class="xref">CapyKit.Password</span> object using the PBKDF2 algorithm with the provided <code class="paramref">password</code>
and <code class="paramref">salt</code>.</p>
<pre><code class="lang-csharp">public static Password Pbkdf2(string password, byte[] salt)
</code></pre>
<h4 id="parameters-9">Parameters</h4>
<p><code>password</code> <a href="https://learn.microsoft.com/dotnet/api/system.string">string</a></p>
<p>The clear text password to be hashed.</p>
<p><code>salt</code> <a href="https://learn.microsoft.com/dotnet/api/system.byte">byte</a>[]</p>
<p>A random value used to add an additional layer of security to the generated hash.</p>
<h4 id="returns-9">Returns</h4>
<p><a href="CapyKit.Password.html">Password</a></p>
<p>A new <span class="xref">CapyKit.Password</span> object containing the hashed password and salt.</p>
<h4 id="remarks-2">Remarks</h4>
<p>This method uses the PBKDF2 (Password-Based Key Derivation Function 2) algorithm to generate
a new password hash. The algorithm iteratively applies a pseudorandom function to the
password and salt, which increases the security of the resulting hash.</p>
<h3 id="CapyKit_Helpers_SecurityHelper_Pbkdf2_System_String_"> Pbkdf2(string)</h3>
<p>Generates a new <span class="xref">CapyKit.Password</span> object using the PBKDF2 algorithm with the provided <code class="paramref">password</code>.
This overload of the method generates a random salt value for added security.</p>
<pre><code class="lang-csharp">public static Password Pbkdf2(string password)
</code></pre>
<h4 id="parameters-10">Parameters</h4>
<p><code>password</code> <a href="https://learn.microsoft.com/dotnet/api/system.string">string</a></p>
<p>The clear text password to be hashed.</p>
<h4 id="returns-10">Returns</h4>
<p><a href="CapyKit.Password.html">Password</a></p>
<p>A new <span class="xref">CapyKit.Password</span> object containing the hashed password and a randomly generated salt.</p>
<h4 id="remarks-3">Remarks</h4>
<p>This method uses the PBKDF2 (Password-Based Key Derivation Function 2) algorithm to generate
a new password hash. The algorithm iteratively applies a pseudorandom function to the
password and salt, which increases the security of the resulting hash. In this overload,
a random salt value is generated using <span class="xref">CapyKit.Helpers.SecurityHelper.GetRandomBytes(System.Int32)</span> method.</p>
</article>
<div class="contribution d-print-none">
</div>
<div class="next-article d-print-none border-top" id="nextArticle"></div>
</div>
<div class="affix">
<nav id="affix"></nav>
</div>
</main>
<div class="container-xxl search-results" id="search-results"></div>
<footer class="border-top text-secondary">
<div class="container-xxl">
<div class="flex-fill">
<span>Made with <a href="https://dotnet.github.io/docfx">docfx</a></span>
</div>
</div>
</footer>
</body>
</html>