Exemplo n.º 1
0
 public override void Put(byte[] key, byte[][] value)
 {
     if (value == default(byte[][]))
     {
         baseStorage.Put(key, null);
     }
     else
     {
         baseStorage.Put(key, Decode(value));
     }
 }
 public override void Put(byte[] key, byte[] value)
 {
     if (value == null)
     {
         baseStorage.Put(key, null);
     }
     else
     {
         baseStorage.Put(key, Encrypt(value));
     }
 }
Exemplo n.º 3
0
        public bool UpdatePassword(byte[] userID, string password, string newPassword, string pepper)
        {
            if (userID == null)
            {
                throw new ArgumentNullException("userID");
            }
            if (password == null)
            {
                throw new ArgumentNullException("password");
            }
            if (newPassword == null)
            {
                throw new ArgumentNullException("newPassword");
            }
            if (pepper == null)
            {
                throw new ArgumentNullException("pepper");
            }
            byte[] privateKeyRaw = userKeys.Get(userID);

            {            //verify password
                System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(privateKeyRaw);
                Stream        cryptoStream          = CreateCryptoStream(memoryStream, userID, password, pepper, CryptoStreamMode.Read);
                RSAParameters privateKey            = DeserializeKey(cryptoStream);
                RSAParameters pubKey = GetPublicKey(userID);
                if ((ByteSequenceComparer.Shared.Compare(pubKey.Modulus, privateKey.Modulus) != 0) || (ByteSequenceComparer.Shared.Compare(pubKey.Exponent, privateKey.Exponent) != 0))
                {
                    return(false);
                }
            }

            MemoryStream newKeyStream = new MemoryStream();
            Rijndael     oldPassRij   = CreateRijndael(userID, password, pepper);
            Rijndael     newPassRij   = CreateRijndael(userID, newPassword, pepper);

            System.Security.Cryptography.CryptoStream streamI = new CryptoStream(new System.IO.MemoryStream(privateKeyRaw), oldPassRij.CreateDecryptor(), CryptoStreamMode.Read);
            System.Security.Cryptography.CryptoStream streamO = new CryptoStream(newKeyStream, newPassRij.CreateEncryptor(), CryptoStreamMode.Write);
            var buffer = new byte[1024];
            var read   = streamI.Read(buffer, 0, buffer.Length);

            while (read > 0)
            {
                streamO.Write(buffer, 0, read);
                read = streamI.Read(buffer, 0, buffer.Length);
            }
            streamO.FlushFinalBlock();

            byte[] newKeyRaw = newKeyStream.ToArray();
            userKeys.Put(userID, newKeyRaw);
            return(true);
        }
Exemplo n.º 4
0
 public void Push(byte[] chunkID, byte[] data, byte[][] dependencies, byte[][] signatures)
 {
     if (chunkID == null)
     {
         throw new ArgumentNullException("chunkID");
     }
     if (data == null)
     {
         throw new ArgumentNullException("data");
     }
     if (dependencies == null)
     {
         throw new ArgumentNullException("dependencies");
     }
     ldataDependencies.Put(chunkID, dependencies);
     ldata.Put(chunkID, data);
     ldataTopLevels.Put(chunkID, dependencies);
     lsignatures.Put(chunkID, signatures);
     for (int n = 0; n != signatures.Length; n++)
     {
         if (signatures [n] == null)
         {
             throw new ArgumentNullException(string.Format("signatures[{0}]", n), "signature cannot be null");
         }
     }
     for (int n = 0; n != dependencies.Length; n++)
     {
         byte[] dependency = dependencies [n];
         if (dependency == null)
         {
             throw new ArgumentNullException(string.Format("dependencies[{0}]", n), "dependency cannot be null");
         }
         ldataTopLevels.Delete(dependency);
     }
 }
Exemplo n.º 5
0
        public void CreateUser(string name, string password, string pepper, byte[] parentID)
        {
            byte[] userID = new byte[32];
            RandomNumberGenerator.Create().GetBytes(userID);
            Console.Write("User ID:");
            foreach (byte b in userID)
            {
                Console.Write(" {0:X2}", b);
            }
            Console.WriteLine();

            using (var rsa = new RSACryptoServiceProvider(2048)) {
                using (var rsaSign = new RSACryptoServiceProvider(2048)) {
                    try {
                        userNames.Put(userID, name);
                        if (parentID != null)
                        {
                            userParents.Put(userID, parentID);
                        }
                        var Rij = CreateRijndael(userID, password, pepper);
                        {
                            MemoryStream streamI      = new MemoryStream(System.Text.Encoding.Unicode.GetBytes(rsa.ToXmlString(true)));
                            MemoryStream newKeyStream = new MemoryStream();
                            CryptoStream streamO      = new CryptoStream(newKeyStream, Rij.CreateEncryptor(), CryptoStreamMode.Write);
                            var          buffer       = new byte[1024];
                            var          read         = streamI.Read(buffer, 0, buffer.Length);
                            while (read > 0)
                            {
                                streamO.Write(buffer, 0, read);
                                read = streamI.Read(buffer, 0, buffer.Length);
                            }
                            streamO.FlushFinalBlock();
                            userKeys.Put(userID, newKeyStream.ToArray());
                        }
                        userCerts.Put(userID, System.Text.Encoding.Unicode.GetBytes(rsa.ToXmlString(false)));

                        {
                            MemoryStream streamI      = new MemoryStream(System.Text.Encoding.Unicode.GetBytes(rsaSign.ToXmlString(true)));
                            MemoryStream newKeyStream = new MemoryStream();
                            CryptoStream streamO      = new CryptoStream(newKeyStream, Rij.CreateEncryptor(), CryptoStreamMode.Write);
                            var          buffer       = new byte[1024];
                            var          read         = streamI.Read(buffer, 0, buffer.Length);
                            while (read > 0)
                            {
                                streamO.Write(buffer, 0, read);
                                read = streamI.Read(buffer, 0, buffer.Length);
                            }
                            streamO.FlushFinalBlock();
                            userSigningKeys.Put(userID, newKeyStream.ToArray());
                        }
                        userSigningCerts.Put(userID, System.Text.Encoding.Unicode.GetBytes(rsaSign.ToXmlString(false)));
                    } finally {
                        rsa.PersistKeyInCsp     = false;
                        rsaSign.PersistKeyInCsp = false;
                    }
                }
            }
        }
 public override void Put(byte[] key, byte[] value)
 {
     baseStorage.Put(key, Encrypt(value));
 }