-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathCryptoRepository.cs
43 lines (35 loc) · 1 KB
/
CryptoRepository.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System;
using System.Collections.Generic;
namespace CryptoShredding.Repository;
public class CryptoRepository
{
private readonly IDictionary<string, EncryptionKey> _cryptoStore;
public CryptoRepository()
{
_cryptoStore = new Dictionary<string, EncryptionKey>();
}
public EncryptionKey GetExistingOrNew(string id, Func<EncryptionKey> keyGenerator)
{
var isExisting = _cryptoStore.TryGetValue(id, out var keyStored);
if (isExisting)
{
return keyStored;
}
var newEncryptionKey = keyGenerator.Invoke();
_cryptoStore.Add(id, newEncryptionKey);
return newEncryptionKey;
}
public EncryptionKey GetExistingOrDefault(string id)
{
var isExisting = _cryptoStore.TryGetValue(id, out var keyStored);
if (isExisting)
{
return keyStored;
}
return default;
}
public void DeleteEncryptionKey(string id)
{
_cryptoStore.Remove(id);
}
}