Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PersistedHashes.cs
1using System;
2using System.Threading.Tasks;
7
9{
13 public static class PersistedHashes
14 {
20 public static Task<bool> AddHash(byte[] Hash)
21 {
22 return AddHash(string.Empty, DateTime.MaxValue, Hash);
23 }
24
31 public static Task<bool> AddHash(DateTime ExpiresUtc, byte[] Hash)
32 {
33 return AddHash(string.Empty, ExpiresUtc, Hash);
34 }
35
42 public static Task<bool> AddHash(string Realm, byte[] Hash)
43 {
44 return AddHash(Realm, DateTime.MaxValue, Hash);
45 }
46
54 public static Task<bool> AddHash(string Realm, DateTime ExpiresUtc, byte[] Hash)
55 {
56 return AddHash(Realm, ExpiresUtc, Hash, null);
57 }
58
66 public static Task<bool> AddHash(string Realm, byte[] Hash, object AssociatedObject)
67 {
68 return AddHash(Realm, DateTime.MaxValue, Hash, AssociatedObject);
69 }
70
79 public static async Task<bool> AddHash(string Realm, DateTime ExpiresUtc, byte[] Hash,
80 object AssociatedObject)
81 {
82 using (Semaphore Semaphore = await Semaphores.BeginWrite("hashrealm:" + Realm))
83 {
84 PersistedHash Obj = await Database.FindFirstDeleteRest<PersistedHash>(
85 new FilterAnd(
86 new FilterFieldEqualTo("Realm", Realm),
87 new FilterFieldEqualTo("Hash", Hash)));
88
89 if (!(Obj is null))
90 return false;
91
92 Obj = new PersistedHash()
93 {
94 Realm = Realm,
95 ExpiresUtc = ExpiresUtc.ToUniversalTime(),
96 Hash = Hash,
97 AssociatedObject = AssociatedObject
98 };
99
100 await Database.Insert(Obj);
101
102 return true;
103 }
104 }
105
112 public static Task<bool> VerifyHash(byte[] Hash)
113 {
114 return VerifyHash(string.Empty, Hash);
115 }
116
123 public static async Task<bool> VerifyHash(string Realm, byte[] Hash)
124 {
125 using (Semaphore Semaphore = await Semaphores.BeginRead("hashrealm:" + Realm))
126 {
127 PersistedHash Obj = await Database.FindFirstDeleteRest<PersistedHash>(
128 new FilterAnd(
129 new FilterFieldEqualTo("Realm", Realm),
130 new FilterFieldEqualTo("Hash", Hash)));
131
132 return !(Obj is null);
133 }
134 }
135
142 public static Task<object> TryGetAssociatedObject(byte[] Hash)
143 {
144 return TryGetAssociatedObject(string.Empty, Hash);
145 }
146
154 public static async Task<object> TryGetAssociatedObject(string Realm, byte[] Hash)
155 {
156 using (Semaphore Semaphore = await Semaphores.BeginRead("hashrealm:" + Realm))
157 {
158 PersistedHash Obj = await Database.FindFirstDeleteRest<PersistedHash>(
159 new FilterAnd(
160 new FilterFieldEqualTo("Realm", Realm),
161 new FilterFieldEqualTo("Hash", Hash)));
162
163 return Obj?.AssociatedObject;
164 }
165 }
166 }
167}
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:21
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
Definition: Database.cs:97
This filter selects objects that conform to all child-filters provided.
Definition: FilterAnd.cs:10
This filter selects objects that have a named field equal to a given value.
Contains information about a persisted hash
Static class managing persistent counters.
static async Task< bool > AddHash(string Realm, DateTime ExpiresUtc, byte[] Hash, object AssociatedObject)
Persists a hash.
static async Task< bool > VerifyHash(string Realm, byte[] Hash)
Verifies if a hash value has been persisted in the database.
static Task< bool > VerifyHash(byte[] Hash)
Verifies if a hash value has been persisted in the database, using the default (empty) realm.
static Task< bool > AddHash(DateTime ExpiresUtc, byte[] Hash)
Persists a hash, using the default (empty) realm.
static Task< bool > AddHash(string Realm, byte[] Hash)
Persists a hash.
static Task< object > TryGetAssociatedObject(byte[] Hash)
Tries to get the associated object of a hash value that has been persisted in the database,...
static Task< bool > AddHash(string Realm, DateTime ExpiresUtc, byte[] Hash)
Persists a hash.
static Task< bool > AddHash(byte[] Hash)
Persists a hash, using the default (empty) realm.
static async Task< object > TryGetAssociatedObject(string Realm, byte[] Hash)
Tries to get the associated object of a hash value that has been persisted in the database.
static Task< bool > AddHash(string Realm, byte[] Hash, object AssociatedObject)
Persists a hash.
Represents a named semaphore, i.e. an object, identified by a name, that allows single concurrent wri...
Definition: Semaphore.cs:19
Static class of application-wide semaphores that can be used to order access to editable objects.
Definition: Semaphores.cs:17
static async Task< Semaphore > BeginRead(string Key)
Waits until the semaphore identified by Key is ready for reading. Each call to BeginRead must be fol...
Definition: Semaphores.cs:54
static async Task< Semaphore > BeginWrite(string Key)
Waits until the semaphore identified by Key is ready for writing. Each call to BeginWrite must be fo...
Definition: Semaphores.cs:91