Пример #1
0
        public void SignMetadata(string prikey)
        {
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

            RSA.FromXmlString(prikey);
            startTime  = StreamFactory.NowUtc();
            duration   = long.MaxValue;
            SignedHash = RSA.SignData(GetDataToHash(), new SHA256CryptoServiceProvider());
            FlushIndexMetaData();
        }
Пример #2
0
        public bool Close(bool retainIndex = false)
        {
            if (logger != null)
            {
                logger.Log("Start ValueDataStream Close");
            }
            if (logger != null)
            {
                logger.Log("Start ValueDataStream File Close");
            }
            if (fs_bw != null)
            {
                if ((streamop == StreamFactory.StreamOp.Write) && !isSealed)
                {
                    fout.Flush(true);
                }
                fs_bw.Close();
                fs_bw = null;
            }

            if (fs_br != null)
            {
                fs_br.Close();
                fs_br = null;
            }
            if (logger != null)
            {
                logger.Log("End ValueDataStream File Close");
            }

            if (!isClosed)
            {
                if (!isSealed && (streamop == StreamFactory.StreamOp.Write))
                {
                    t_e = StreamFactory.NowUtc();
                    Flush(retainIndex);
                    Sync();
                }
                else if (streamop == StreamFactory.StreamOp.Read)
                {
                    Flush(retainIndex);
                }
            }

            isClosed = true;
            if (logger != null)
            {
                logger.Log("End ValueDataStream Close");
            }
            return(isClosed);
        }
Пример #3
0
        public static ByteValue DecryptDataAES(IValue cipherText, byte[] Key, byte[] IV)
        {
            // Check arguments.
            if (cipherText == null || cipherText.ToString().Length <= 0)
            {
                throw new ArgumentNullException("cipherText");
            }
            if (Key == null || Key.Length <= 0)
            {
                throw new ArgumentNullException("Key");
            }
            if (IV == null || IV.Length <= 0)
            {
                throw new ArgumentNullException("Key");
            }

            // Declare the string used to hold
            // the decrypted text.
            string plaintext = null;

            // Create an AesManaged object
            // with the specified key and IV.
            using (AesManaged aesAlg = new AesManaged())
            {
                aesAlg.Key = Key;
                aesAlg.IV  = IV;

                // Create a decrytor to perform the stream transform.
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                // Create the streams used for decryption.
                using (MemoryStream msDecrypt = new MemoryStream(StreamFactory.GetBytes(cipherText.ToString())))
                // using (MemoryStream msDecrypt = new MemoryStream(cipherText.GetBytes()))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            // Read the decrypted bytes from the decrypting stream
                            // and place them in a string.
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }
            }
            return(new ByteValue(StreamFactory.GetBytes(plaintext)));
        }
Пример #4
0
        public new void Append(List <Tuple <IKey, IValue> > list)
        {
            long timestamp = StreamFactory.NowUtc();

            foreach (Tuple <IKey, IValue> keyValPair in list)
            {
                if (logger != null)
                {
                    logger.Log("Start ValueDataStream Append");
                }
                UpdateHelper(keyValPair.Item1, keyValPair.Item2, true, timestamp);
                if (logger != null)
                {
                    logger.Log("End ValueDataStream Append");
                }
            }
        }
Пример #5
0
        public void Append(List <IKey> listOfKeys, IValue value)
        {
            long offset    = -1;
            long timestamp = StreamFactory.NowUtc();

            foreach (IKey key in listOfKeys)
            {
                if (logger != null)
                {
                    logger.Log("Start ValueDataStream Append");
                }
                offset = UpdateHelper(key, value, true, null, timestamp, offset);
                if (logger != null)
                {
                    logger.Log("End ValueDataStream Append");
                }
            }
        }
Пример #6
0
 public void SetBytes(byte[] keyBytes)
 {
     this.key = StreamFactory.GetString(keyBytes);
 }
Пример #7
0
 public byte[] GetBytes()
 {
     return(StreamFactory.GetBytes(this.key));
 }
Пример #8
0
 public void SetBytes(byte[] valBytes)
 {
     this.val = StreamFactory.GetASCIIString(valBytes);
 }
Пример #9
0
 public byte[] GetBytes()
 {
     return(StreamFactory.GetASCIIBytes(val));
 }
Пример #10
0
 public override string ToString()
 {
     return(StreamFactory.GetString(val));
 }
Пример #11
0
        protected Tuple <Byte[], StrValue> UpdateHelper(IKey key, IValue value, bool IsAppend, long timestamp = -1)
        {
            if (!value.GetType().Equals(typeof(ByteValue)))
            {
                throw new InvalidDataException("Invalid IValue Type.  ByteValue expected.");
            }

            if (logger != null)
            {
                logger.Log("Start FileDataStream Delete Old DataBlock");
            }
            // check if the entry is present, so that the old file can be deleted
            IValue valueDataFilePathOld = base.Get(key);

            // remove old entry/file if present and update has been called
            if (valueDataFilePathOld != null && !IsAppend)
            {
                System.IO.FileInfo fi = new System.IO.FileInfo(valueDataFilePathOld.ToString());
                try
                {
                    fi.Delete();
                }
                catch (System.IO.IOException e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            if (logger != null)
            {
                logger.Log("End FileDataStream Delete Old DataBlock");
            }



            if (logger != null)
            {
                logger.Log("Start FileDataStream Construct DataBlock");
            }
            long ts;  // timestamp

            ts = StreamFactory.HighResTick();
            string   dataFilePath         = targetDir + "/" + Convert.ToString(ts) + ".dat";
            StrValue strDataFilePathValue = new StrValue(Convert.ToString(ts) + ".dat");

            if (streamtype == StreamFactory.StreamSecurityType.Secure)
            {
                value = new ByteValue(Crypto.EncryptBytesSimple(value.GetBytes(), Crypto.KeyDer(acl_md.encKey), acl_md.IV));
            }
            else
            {
                value = new ByteValue(value.GetBytes());
            }


            // if (logger != null) logger.Log("Start FileDataStream Construct DBI");
            Byte[] hash;
            if (streamtype == StreamFactory.StreamSecurityType.Secure)
            {
                hash = hasher.ComputeHash(value.GetBytes());
            }
            else
            {
                hash = null;
            }
            // if (logger != null) logger.Log("End FileDataStream Construct DBI");
            if (logger != null)
            {
                logger.Log("End FileDataStream Construct DataBlock");
            }

            if (logger != null)
            {
                logger.Log("Start FileDataStream Update FilePathValue");
            }
            base.UpdateHelper(key, strDataFilePathValue, IsAppend, hash, timestamp);
            if (logger != null)
            {
                logger.Log("End FileDataStream Update FilePathValue");
            }

            // write <val> to file
            if (logger != null)
            {
                logger.Log("Start FileDataStream WriteToDisc DataBlock");
            }
            FileStream fout = new FileStream(dataFilePath,
                                             FileMode.OpenOrCreate,
                                             FileAccess.Write,
                                             FileShare.ReadWrite);

            fout.Write(value.GetBytes(), 0, (int)value.Size());
            fout.Flush(true);
            fout.Close();
            if (logger != null)
            {
                logger.Log("End FileDataStream WriteToDisc DataBlock");
            }

            return(new Tuple <byte[], StrValue>(hash, strDataFilePathValue));
        }
Пример #12
0
        protected long UpdateHelper(IKey key, IValue value, bool IsAppend, Byte[] valHash = null, long timestamp = -1, long offsetInStream = -1)
        {
            long offset;

            if (!key.GetType().Equals(typeof(KeyType)))
            {
                throw new InvalidDataException("Invalid IKey Type");
            }
            if (!value.GetType().Equals(typeof(ValType)))
            {
                throw new InvalidDataException("Invalid IValue Type");
            }

            if (logger != null)
            {
                logger.Log("Start ValueDataStream Tag Lookup");
            }
            List <DataBlockInfo> offsets;

            // check if the entry is present (and record offset)
            if (index.ContainsKey(key))
            {
                // TODO: this could just have been a simple collision
                //   -- make sure that this is the same key
                //   -- if different, use a secondary hash function

                offsets = index[key];
            }
            else
            {
                offsets    = new List <DataBlockInfo>();
                index[key] = offsets;
                // IncrementIndexSize(offsets.Count);
                IncrementIndexSize(1);
            }
            if (logger != null)
            {
                logger.Log("End ValueDataStream Tag Lookup");
            }

            if (logger != null)
            {
                logger.Log("Start ValueDataStream Construct DataBlock");
            }
            // write <op (1B), ts, key, val_len, val>
            byte op;

            if (IsAppend)
            {
                op = (byte)WriteOp.AppendOp;
            }
            else
            {
                op = (byte)WriteOp.UpdateOp;
            }


            long ts;

            // Construct datablock
            if (timestamp == -1)
            {
                ts = StreamFactory.NowUtc();
            }
            else
            {
                ts = timestamp;
            }

            DataBlock <KeyType, ValType> db = new DataBlock <KeyType, ValType>();

            db.op = op;
            //db.timestamp = ts;
            //db.setKey(key);

            /*
             * Commenting out because the synchronizer will do this now at the chunk level
             * if (streamtype == StreamFactory.StreamSecurityType.Secure)
             * {
             *  //if (logger != null) logger.Log("Start encrypting value");
             *  db.value = new ByteValue(Crypto.EncryptBytesSimple(value.GetBytes(), Crypto.KeyDer(acl_md.encKey), acl_md.IV));
             *  //if (logger != null) logger.Log("End encrypting value");
             * }
             * else*/
            {
                //db.value = new ByteValue(value.GetBytes());
                db.setValue(value);
            }
            if (logger != null)
            {
                logger.Log("End ValueDataStream Construct DataBlock");
            }

            if (offsetInStream == -1)
            {
                if (logger != null)
                {
                    logger.Log("Start ValueDataStream Serialize DataBlock");
                }
                //Byte[] buffer = db.SerializeToByteStream().ToArray();
                Byte[] buffer = SerializerHelper <DataBlock <KeyType, ValType> > .SerializeToProtoStream(db).ToArray();

                if (logger != null)
                {
                    logger.Log("End ValueDataStream Serialize DataBlock");
                }

                if (logger != null)
                {
                    logger.Log("Start ValueDataStream WriteToDisc DataBlock");
                }
                // fs_bw.Write(db.SerializeToJsonStream());
                // get file offset; add <key_hash, offset> to index
                fs_bw.BaseStream.Seek(0, SeekOrigin.End);
                offset = fs_bw.BaseStream.Position;
                fs_bw.Write(buffer.Length);
                fs_bw.Write(buffer);
                fs_bw.Flush();
                if (logger != null)
                {
                    logger.Log("End ValueDataStream WriteToDisc DataBlock");
                }
            }
            else
            {
                offset = offsetInStream;
            }
            // Construct dbinfo in index
            if (logger != null)
            {
                logger.Log("Start ValueDataStream Construct and Add DataBlockInfo");
            }
            DataBlockInfo latest_tso = new DataBlockInfo(ts, offset);

            if (streamtype == StreamFactory.StreamSecurityType.Secure)
            {
                if (valHash != null) // passed from the dir stream and written into datablockinfo. not written for file stream
                {
                    latest_tso.hashValue = valHash;
                }

                /*
                 *  else
                 *  {
                 *      if (logger != null) logger.Log("Start taking hash of encrypted value");
                 *      latest_tso.hashValue = hasher.ComputeHash(db.value.GetBytes());
                 *      if (logger != null) logger.Log("End taking hash of encrypted value");
                 *
                 *  }
                 *   latest_tso.key_version = acl_md.keyVersion;
                 */
            }

            // apply to index
            if (IsAppend)
            {
                offsets.Add(latest_tso);
                // IncrementIndexSize();
            }
            else
            {
                if (offsets.Count == 0)
                {
                    offsets.Add(latest_tso);
                    // IncrementIndexSize();
                }
                else
                {
                    offsets[offsets.Count - 1] = latest_tso;
                }
            }
            if (logger != null)
            {
                logger.Log("End ValueDataStream Construct and Add DataBlockInfo");
            }
            return(offset);
        }