Electro.StatePersister.SaveKey C# (CSharp) Méthode

SaveKey() public méthode

public SaveKey ( System.Guid electionId, PrivateKey privateKey ) : void
electionId System.Guid
privateKey Electro.Crypto.PrivateKey
Résultat void
        public void SaveKey(Guid electionId, PrivateKey privateKey)
        {
            if(keysWriter == null)
            {
                lock(keysFilePath)
                {
                    if(keysWriter == null)
                        keysWriter = new StreamWriter(new FileStream(keysFilePath, FileMode.Append)) { AutoFlush = true };
                }
            }

            keysWriter.WriteLine(new KeyValuePair<Guid, PrivateKey>(electionId, privateKey).ToJsonString());
        }

Usage Example

Exemple #1
0
        public Guid StartElection(string electionName, User firstCandidate, bool isPublic, DateTime nominateTill, DateTime till)
        {
            var homoKeyPair = HomoKeyPair.GenKeyPair(MaxVotesPerElection);
            var election    = new Election
            {
                Id           = Guid.NewGuid(),
                Name         = electionName,
                NominateTill = nominateTill,
                VoteTill     = till,
                PublicKey    = homoKeyPair.PublicKey,
                Votes        = new List <Vote>(),
                Candidates   = new List <CandidateInfo> {
                    CandidateInfo.Create(firstCandidate)
                },
                IsPublic = isPublic
            };

            statePersister.SaveKey(election.Id, homoKeyPair.PrivateKey);

            lock (electionsList)
            {
                electionPrivateKeys[election.Id] = homoKeyPair.PrivateKey;
                electionsDict[election.Id]       = election;

                electionsList.AddFirst(election);
                while (electionsList.Count > MaxElections)
                {
                    var      node = electionsList.Last;
                    Election dummy;
                    electionsDict.TryRemove(node.Value.Id, out dummy);
                    electionsList.RemoveLast();
                }
            }

            return(election.Id);
        }