-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Libraries.ConfigurationManager): Kryptographiedienst zum verschl…
…üsseln und entschlüsseln mit Passwort hinzugefügt
- Loading branch information
Showing
2 changed files
with
46 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/coIT.Libraries.ConfigurationManager/Cryptography/PasswordCryptographyService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System.Security.Cryptography; | ||
using CSharpFunctionalExtensions; | ||
|
||
namespace coIT.Libraries.ConfigurationManager.Cryptography | ||
{ | ||
public class PasswordCryptographyService : IDoCryptography | ||
{ | ||
private readonly AesCryptographyService _crypgraphyService; | ||
|
||
public PasswordCryptographyService(string password) | ||
{ | ||
var keyAndIv = PasswordToKeyAndIV(password); | ||
_crypgraphyService = new AesCryptographyService(keyAndIv.Key, keyAndIv.IV); | ||
} | ||
|
||
private static (byte[] Key, byte[] IV) PasswordToKeyAndIV(string password) | ||
{ | ||
// Da die Passwörter in Bitwarden liegen und bereits extrem lang sind | ||
// und dieser Code nie großflächig für Invididuelle Passwörter eingesetzt | ||
// werden sollte, kann man entgegen Best Practices hier ohne Salt arbeiten | ||
byte[] salt = [00]; | ||
var schlüsselableiter = new Rfc2898DeriveBytes( | ||
password, | ||
salt, | ||
1000, | ||
HashAlgorithmName.SHA512 | ||
); | ||
|
||
var key = schlüsselableiter.GetBytes(32); | ||
var iv = schlüsselableiter.GetBytes(16); | ||
|
||
return (key, iv); | ||
} | ||
|
||
public Result<string> Decrypt(string encryptedText) | ||
{ | ||
return _crypgraphyService.Decrypt(encryptedText); | ||
} | ||
|
||
public Result<string> Encrypt(string plainText) | ||
{ | ||
return _crypgraphyService.Encrypt(plainText); | ||
} | ||
} | ||
} |