Ejemplo n.º 1
0
        public string hashCal(string type, string str)
        {
            StringBuffer hexString = new StringBuffer();

            try
            {
                MessageDigest digestTxID = null;
                digestTxID = MessageDigest.GetInstance(type);
                digestTxID.Reset();
                digestTxID.Update(Encoding.ASCII.GetBytes(str.ToString()));
                byte[] messageDigest = digestTxID.Digest();

                for (int i = 0; i < messageDigest.Length; i++)
                {
                    string hex = Integer.ToHexString(0xFF & messageDigest[i]);
                    if (hex.Length == 1)
                    {
                        hexString.Append("0");
                    }
                    hexString.Append(hex);
                }
            }
            catch (NoSuchAlgorithmException nsae) { }

            return(hexString.ToString());
        }
Ejemplo n.º 2
0
        protected internal static ISecretKey GenerateSecretKey(String password,
                                                               EncryptionVerifier ver)
        {
            if (password.Length > 255)
            {
                password = password.Substring(0, 255);
            }
            HashAlgorithm hashAlgo = ver.HashAlgorithm;
            MessageDigest hashAlg  = CryptoFunctions.GetMessageDigest(hashAlgo);

            byte[] hash = hashAlg.Digest(StringUtil.GetToUnicodeLE(password));
            byte[] salt = ver.Salt;
            hashAlg.Reset();
            for (int i = 0; i < 16; i++)
            {
                hashAlg.Update(hash, 0, 5);
                hashAlg.Update(salt);
            }

            hash = new byte[5];
            Array.Copy(hashAlg.Digest(), 0, hash, 0, 5);
            ISecretKey skey = new SecretKeySpec(hash, ver.CipherAlgorithm.jceId);

            return(skey);
        }
Ejemplo n.º 3
0
        /// <summary>Create a thread local MD5 digester</summary>
        public static MessageDigest GetDigester()
        {
            MessageDigest digester = DigesterFactory.Get();

            digester.Reset();
            return(digester);
        }
        private void VerifyLooseObject(AnyObjectId id, byte[] compressed)
        {
            UnpackedObjectLoader uol;

            try
            {
                uol = new UnpackedObjectLoader(compressed);
            }
            catch (CorruptObjectException parsingError)
            {
                // Some HTTP servers send back a "200 OK" status with an HTML
                // page that explains the requested file could not be found.
                // These servers are most certainly misconfigured, but many
                // of them exist in the world, and many of those are hosting
                // Git repositories.
                //
                // Since an HTML page is unlikely to hash to one of our loose
                // objects we treat this condition as a FileNotFoundException
                // and attempt to recover by getting the object from another
                // source.
                //
                var e = new FileNotFoundException(id.Name, parsingError);
                throw e;
            }

            _objectDigest.Reset();
            _objectDigest.Update(Constants.encodedTypeString(uol.Type));
            _objectDigest.Update((byte)' ');
            _objectDigest.Update(Constants.encodeASCII(uol.Size));
            _objectDigest.Update(0);
            _objectDigest.Update(uol.CachedBytes);
            _idBuffer.FromRaw(_objectDigest.Digest(), 0);

            if (!AnyObjectId.equals(id, _idBuffer))
            {
                throw new TransportException("Incorrect hash for " + id.Name + "; computed " + _idBuffer.Name + " as a " +
                                             Constants.typeString(uol.Type) + " from " + compressed.Length +
                                             " bytes.");
            }
            if (_objCheck != null)
            {
                try
                {
                    _objCheck.check(uol.Type, uol.CachedBytes);
                }
                catch (CorruptObjectException e)
                {
                    throw new TransportException("Invalid " + Constants.typeString(uol.Type) + " " + id.Name + ": " + e.Message);
                }
            }
        }
Ejemplo n.º 5
0
        /**
         * Construct LogSignatureVerifiers for each of the trusted CT logs.
         *
         * @throws InvalidKeySpecException the CT log key isn't RSA or EC, the key is probably corrupt.
         * @throws NoSuchAlgorithmException the crypto provider couldn't supply the hashing algorithm or
         *     the key algorithm. This probably means you are using an ancient or bad crypto provider.
         */
        private void buildLogSignatureVerifiers()
        {
            MessageDigest hasher = MessageDigest.GetInstance(LOG_ID_HASH_ALGORITHM);

            foreach (string trustedLogKey in TRUSTED_LOG_KEYS)
            {
                hasher.Reset();
                byte[]     keyBytes   = Base64.Decode(trustedLogKey);
                string     logId      = Base64.ToBase64String(hasher.Digest(keyBytes));
                KeyFactory keyFactory = KeyFactory.GetInstance(determineKeyAlgorithm(keyBytes));
                var        publicKey  = keyFactory.GeneratePublic(new X509EncodedKeySpec(keyBytes));
                verifiers.Add(logId, new LogSignatureVerifier(new LogInfo(publicKey)));
            }
        }
 protected byte[] CreateKeyBytes(string key)
 {
     try
     {
         MessageDigest md = MessageDigest.GetInstance(SECRET_KEY_HASH_TRANSFORMATION);
         md.Reset();
         byte[] keyBytes = md.Digest(Encoding.UTF8.GetBytes(key) /*key.GetBytes(CHARSET)*/);
         return(keyBytes);
     }
     catch (UnsupportedEncodingException e)
     {
         throw new SharedPreferencesManagerException(e);
     }
     catch (NoSuchAlgorithmException e)
     {
         throw new SharedPreferencesManagerException(e);
     }
 }
Ejemplo n.º 7
0
        /// <exception cref="System.IO.IOException"></exception>
        private byte[] ComputeHash(InputStream @in, long length)
        {
            MessageDigest contentDigest = state.contentDigest;

            byte[] contentReadBuffer = state.contentReadBuffer;
            contentDigest.Reset();
            contentDigest.Update(hblob);
            contentDigest.Update(unchecked ((byte)' '));
            long sz = length;

            if (sz == 0)
            {
                contentDigest.Update(unchecked ((byte)'0'));
            }
            else
            {
                int bufn = contentReadBuffer.Length;
                int p    = bufn;
                do
                {
                    contentReadBuffer[--p] = digits[(int)(sz % 10)];
                    sz /= 10;
                }while (sz > 0);
                contentDigest.Update(contentReadBuffer, p, bufn - p);
            }
            contentDigest.Update(unchecked ((byte)0));
            for (; ;)
            {
                int r = @in.Read(contentReadBuffer);
                if (r <= 0)
                {
                    break;
                }
                contentDigest.Update(contentReadBuffer, 0, r);
                sz += r;
            }
            if (sz != length)
            {
                return(zeroid);
            }
            return(contentDigest.Digest());
        }
Ejemplo n.º 8
0
        public BlobStoreWriter(BlobStore store)
        {
            this.store = store;
            try
            {
                sha1Digest = MessageDigest.GetInstance("SHA-1");
                sha1Digest.Reset();
                md5Digest = MessageDigest.GetInstance("MD5");
                md5Digest.Reset();
            } catch (NotSupportedException e) {
                throw Misc.CreateExceptionAndLog(Log.To.Database, e, Tag,
                                                 "Could not get an instance of SHA-1 or MD5 for BlobStoreWriter.");
            }

            try {
                OpenTempFile();
            } catch (FileNotFoundException e) {
                throw Misc.CreateExceptionAndLog(Log.To.Database, e, Tag,
                                                 "Unable to open temporary file for BlobStoreWriter.");
            }
        }
Ejemplo n.º 9
0
        private void VerifyLooseObject(AnyObjectId id, byte[] compressed)
        {
            UnpackedObjectLoader uol;

            try
            {
                uol = new UnpackedObjectLoader(compressed);
            }
            catch (CorruptObjectException parsingError)
            {
                var e = new FileNotFoundException(id.Name, parsingError);
                throw e;
            }

            _objectDigest.Reset();
            _objectDigest.Update(Constants.encodedTypeString(uol.Type));
            _objectDigest.Update((byte)' ');
            _objectDigest.Update(Constants.encodeASCII(uol.Size));
            _objectDigest.Update(0);
            _objectDigest.Update(uol.CachedBytes);
            _idBuffer.FromRaw(_objectDigest.Digest(), 0);

            if (!id.Equals(_idBuffer))
            {
                throw new TransportException("Incorrect hash for " + id.Name + "; computed " + _idBuffer.Name + " as a " +
                                             Constants.typeString(uol.Type) + " from " + compressed.Length +
                                             " bytes.");
            }
            if (_objCheck != null)
            {
                try
                {
                    _objCheck.check(uol.Type, uol.CachedBytes);
                }
                catch (CorruptObjectException e)
                {
                    throw new TransportException("Invalid " + Constants.typeString(uol.Type) + " " + id.Name + ": " + e.Message);
                }
            }
        }
Ejemplo n.º 10
0
 public BlobStoreWriter(BlobStore store)
 {
     this.store = store;
     try
     {
         sha1Digest = MessageDigest.GetInstance("SHA-1");
         sha1Digest.Reset();
         md5Digest = MessageDigest.GetInstance("MD5");
         md5Digest.Reset();
     }
     catch (NoSuchAlgorithmException e)
     {
         throw new InvalidOperationException("Could not get an instance of SHA-1 or MD5.", e);
     }
     try
     {
         OpenTempFile();
     }
     catch (FileNotFoundException e)
     {
         throw new InvalidOperationException("Unable to open temporary file.", e);
     }
 }
Ejemplo n.º 11
0
		public BlobStoreWriter(BlobStore store)
		{
			this.store = store;
			try
			{
				sha1Digest = MessageDigest.GetInstance("SHA-1");
				sha1Digest.Reset();
				md5Digest = MessageDigest.GetInstance("MD5");
				md5Digest.Reset();
			}
			catch (NoSuchAlgorithmException e)
			{
				throw new InvalidOperationException(e);
			}
			try
			{
				OpenTempFile();
			}
			catch (FileNotFoundException e)
			{
				throw new InvalidOperationException(e);
			}
		}
 public BlobStoreWriter(BlobStore store)
 {
     this.store = store;
     try
     {
         sha1Digest = MessageDigest.GetInstance("SHA-1");
         sha1Digest.Reset();
         md5Digest = MessageDigest.GetInstance("MD5");
         md5Digest.Reset();
     }
     catch (NoSuchAlgorithmException e)
     {
         throw new InvalidOperationException(e);
     }
     try
     {
         OpenTempFile();
     }
     catch (FileNotFoundException e)
     {
         throw new InvalidOperationException(e);
     }
 }
Ejemplo n.º 13
0
 protected void EngineReset()
 {
     _md5.Reset();
     _md5.Update(_ipad);
 }
 /// <returns>digest to help compute an ObjectId</returns>
 protected internal virtual MessageDigest Digest()
 {
     digest.Reset();
     return(digest);
 }
Ejemplo n.º 15
0
		public BlobStoreWriter(BlobStore store)
		{
			this.store = store;
			try
			{
				sha1Digest = MessageDigest.GetInstance("SHA-1");
				sha1Digest.Reset();
				md5Digest = MessageDigest.GetInstance("MD5");
				md5Digest.Reset();
			}
			catch (NoSuchAlgorithmException e)
			{
                throw new InvalidOperationException("Could not get an instance of SHA-1 or MD5." , e);
			}
			try
			{
				OpenTempFile();
			}
			catch (FileNotFoundException e)
			{
                throw new InvalidOperationException("Unable to open temporary file.", e);
			}
		}
Ejemplo n.º 16
0
        internal ObjectId WriteObject(ObjectType type, long len, Stream input, bool store)
        {
            FileInfo             info;
            DeflaterOutputStream stream;
            FileStream           stream2;
            ObjectId             objectId = null;

            if (store)
            {
                info    = _r.ObjectsDirectory.CreateTempFile("noz");
                stream2 = info.OpenWrite();
            }
            else
            {
                info    = null;
                stream2 = null;
            }

            _md.Reset();
            if (store)
            {
                _def.Reset();
                stream = new DeflaterOutputStream(stream2, _def);
            }
            else
            {
                stream = null;
            }

            try
            {
                int    num;
                byte[] bytes = Codec.EncodedTypeString(type);
                _md.Update(bytes);
                if (stream != null)
                {
                    stream.Write(bytes, 0, bytes.Length);
                }

                _md.Update(0x20);
                if (stream != null)
                {
                    stream.WriteByte(0x20);
                }

                bytes = Constants.encodeASCII(len.ToString());
                _md.Update(bytes);
                if (stream != null)
                {
                    stream.Write(bytes, 0, bytes.Length);
                }

                _md.Update(0);
                if (stream != null)
                {
                    stream.WriteByte(0);
                }
                while ((len > 0L) && ((num = input.Read(_buf, 0, (int)Math.Min(len, _buf.Length))) > 0))
                {
                    _md.Update(_buf, 0, num);
                    if (stream != null)
                    {
                        stream.Write(_buf, 0, num);
                    }
                    len -= num;
                }

                if (len != 0L)
                {
                    throw new IOException("Input did not match supplied Length. " + len + " bytes are missing.");
                }

                if (stream != null)
                {
                    stream.Close();
                    if (info != null)
                    {
                        info.IsReadOnly = true;
                    }
                }
                objectId = ObjectId.FromRaw(_md.Digest());
            }
            finally
            {
                if ((objectId == null) && (stream != null))
                {
                    try
                    {
                        stream.Close();
                    }
                    finally
                    {
                        info.DeleteFile();
                    }
                }
            }
            if (info != null)
            {
                if (_r.HasObject(objectId))
                {
                    // Object is already in the repository so remove
                    // the temporary file.
                    //
                    info.DeleteFile();
                }
                else
                {
                    FileInfo info2 = _r.ToFile(objectId);
                    if (!info.RenameTo(info2.FullName))
                    {
                        // Maybe the directory doesn't exist yet as the object
                        // directories are always lazily created. Note that we
                        // try the rename first as the directory likely does exist.
                        //
                        if (info2.Directory != null)
                        {
                            info2.Directory.Create();
                        }
                        if (!info.RenameTo(info2.FullName) && !_r.HasObject(objectId))
                        {
                            // The object failed to be renamed into its proper
                            // location and it doesn't exist in the repository
                            // either. We really don't know what went wrong, so
                            // fail.
                            //
                            info.DeleteFile();
                            throw new ObjectWritingException("Unable to create new object: " + info2);
                        }
                    }
                }
            }
            return(objectId);
        }