/**
         * Updates MD5 hash result.
         */
        private static void update(MessageDigest resultDigest,
                                   byte[] digest)
        {
            for (int i = 0; i < digest.length; i++)
            {
                int d1 = (digest[i] >> 4) & 0xf;
                int d2 = (digest[i] & 0xf);

                resultDigest.update((byte)toHexChar(d1));
                resultDigest.update((byte)toHexChar(d2));
            }
        }
Ejemplo n.º 2
0
 private static sbyte[] Hash(sbyte[] salt, sbyte[] password)
 {
     try
     {
         MessageDigest m = MessageDigest.getInstance(DIGEST_ALGO);
         m.update(salt, 0, salt.Length);
         m.update(password, 0, password.Length);
         return(m.digest());
     }
     catch (NoSuchAlgorithmException e)
     {
         throw new Exception("Hash algorithm is not available on this platform: " + e.Message, e);
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Función encargada de encriptar la contraseña
        /// </summary>
        /// <param name="passwordToHash">contraseña sin cifrar</param>
        /// <returns></returns>
        public string encryptPassword(string passwordToHash)
        {
            string generatedPassword = null;
            string salt = "0uTl@k";

            try
            {
                MessageDigest md = MessageDigest.getInstance("SHA-256");
                md.update(Encoding.UTF8.GetBytes(salt));
                byte[]        bytes = md.digest(Encoding.UTF8.GetBytes(passwordToHash));
                StringBuilder sb    = new StringBuilder();
                for (int i = 0; i < bytes.Length; i++)
                {
                    sb.Append(java.lang.Integer.toString((bytes[i] & 0xff) + 0x100, 16).Substring(1));
                }
                generatedPassword = sb.ToString();
            }
            catch (NoSuchAlgorithmException e)
            {
                e.printStackTrace();
            }
            return(generatedPassword);

            throw new UnsupportedEncodingException();
        }
Ejemplo n.º 4
0
        public virtual int sceMd5BlockUpdate(TPointer contextAddr, TPointer sourceAddr, int size)
        {
            sbyte[] source = getMemoryBytes(sourceAddr.Address, size);
            md5.update(source);

            return(0);
        }
Ejemplo n.º 5
0
		public override string CalculateSHA1() {
			MessageDigest md = MessageDigest.getInstance ("SHA");
			DataBufferInt dbi = (DataBufferInt) Image.getRaster ().getDataBuffer ();
			for (int i=0; i<dbi.getNumBanks (); i++) {
				int [] curBank = dbi.getData (i);
				for (int j=0; j<curBank.Length; j++) {
					int x = curBank[j];
					md.update ((sbyte) (x & 0xFF));
					md.update ((sbyte) ((x>>8) & 0xFF));
					md.update ((sbyte) ((x>>16) & 0xFF));
					md.update ((sbyte) ((x>>24) & 0xFF));
				}
			}
			byte [] resdata = (byte[])vmw.common.TypeUtils.ToByteArray(md.digest());
			return Convert.ToBase64String (resdata);
		}
Ejemplo n.º 6
0
        /// <summary>
        ///  Generate a hash from a source string
        /// </summary>
        internal static string genHash(string strSrc)
        {
            string strHash = "";

            try
            {
                MessageDigest md    = MessageDigest.getInstance("SHA-1");
                sbyte[]       inBuf = strSrc.GetBytes("UTF-8");
                md.update(inBuf);
                sbyte[] finalVal = md.digest();

                // convert the hash value to string
                StringBuilder sb = new StringBuilder(finalVal.Length * 2);
                for (int i = 0; i < finalVal.Length; ++i)
                {
                    sb.Append(hex2Ascii(((int)finalVal[i] >> 4) & 0x0f));
                    sb.Append(hex2Ascii((int)finalVal[i] & 0x0f));
                }

                strHash = sb.ToString();
            }
            catch (Exception e)
            {
                SqlFsLog.debug(e);
            }

            return(strHash);
        }
        /**
         * Updates MD5 hash.
         */
        private static void md5(MessageDigest md,
                                string string)
        {
            int length = string.length();

            for (int i = 0; i < length; i++)
            {
                md.update((byte)string[i]);
            }
        }
Ejemplo n.º 8
0
        protected internal virtual sbyte[] createByteHash(string password)
        {
            MessageDigest digest = createDigestInstance();

            try
            {
                digest.update(password.GetBytes(Encoding.UTF8));
                return(digest.digest());
            }
            catch (UnsupportedEncodingException)
            {
                throw new ProcessEngineException("UnsupportedEncodingException while calculating password digest");
            }
        }
Ejemplo n.º 9
0
 /**
  * 计算消息摘要。
  * @param data 计算摘要的数据。
  * @param offset 数据偏移地址。
  * @param length 数据长度。
  * @return 摘要结果。(16字节)
  */
 public static String md5Str(String str, int offset)
 {
     try
     {
         MessageDigest md5 = MessageDigest.getInstance("MD5");
         byte[]        b   = strToToHexByte(str);
         md5.update(b, offset, b.Length);
         return(byteArrayToHexString(md5.digest()));
     }
     catch (NoSuchAlgorithmException ex)
     {
         ex.printStackTrace();
         return(null);
     }
 }
Ejemplo n.º 10
0
        public virtual Event next()
        {
            Event @event = eventStream.next();

            try
            {
                digest.update(@event.ToString().GetBytes("UTF-8"));
            }
            catch (UnsupportedEncodingException e)
            {
                throw new IllegalStateException("UTF-8 encoding is not available!", e);
            }

            return(@event);
        }
Ejemplo n.º 11
0
 public virtual sbyte[] doSHA1(sbyte[] bytes, int Length)
 {
     try
     {
         MessageDigest md = MessageDigest.getInstance("SHA-1");
         md.update(bytes, 0, Length);
         sbyte[] sha1Hash = md.digest();
         return(sha1Hash);
     }
     catch (Exception e)
     {
         Console.WriteLine(e.ToString());
         Console.Write(e.StackTrace);
         return(null);
     }
 }
Ejemplo n.º 12
0
 public virtual sbyte[] doSHA256(sbyte[] bytes, int lenght)
 {
     try
     {
         MessageDigest md         = MessageDigest.getInstance("SHA-256");
         sbyte[]       sha256Hash = new sbyte[40];
         md.update(bytes, 0, lenght);
         sha256Hash = md.digest();
         return(sha256Hash);
     }
     catch (Exception e)
     {
         Console.WriteLine(e.ToString());
         Console.Write(e.StackTrace);
         return(null);
     }
 }
Ejemplo n.º 13
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: static byte[] encryptPasswordSHA(byte[] userID, byte[] password, byte[] clientSeed, byte[] serverSeed) throws java.io.IOException
        internal static sbyte[] encryptPasswordSHA(sbyte[] userID, sbyte[] password, sbyte[] clientSeed, sbyte[] serverSeed)
        {
            try
            {
                MessageDigest md = MessageDigest.getInstance("SHA");
                md.update(userID);
                md.update(password);
                sbyte[] token = md.digest();
                md.update(token);
                md.update(serverSeed);
                md.update(clientSeed);
                md.update(userID);
                md.update(SHA_SEQUENCE);
                return(md.digest());
            }
            catch (NoSuchAlgorithmException e)
            {
                IOException io = new IOException("Error loading SHA encryption: " + e);
                io.initCause(e);
                throw io;
            }
        }
        /**
         * Appends the authorization string to the StringBuilder.
         */
        private static void appendResponse(StringBuilder sb,
                                           string user,
                                           string realm,
                                           string pass,
                                           string requestMethod,
                                           string uri,
                                           string nonce,
                                           string nc,
                                           string cnonce,
                                           string qop,
                                           string algorithm)
        {
            MessageDigest resultDigest  = null;
            MessageDigest scratchDigest = null;

            try {
                resultDigest  = MessageDigest.getInstance(algorithm);
                scratchDigest = MessageDigest.getInstance(algorithm);
            }
            catch (NoSuchAlgorithmException e) {
                throw new QuercusModuleException(e);
            }

            {
                md5(scratchDigest, user);
                scratchDigest.update((byte)':');

                md5(scratchDigest, realm);
                scratchDigest.update((byte)':');

                md5(scratchDigest, pass);

                update(resultDigest, scratchDigest.digest());
                resultDigest.update((byte)':');
            }

            md5(resultDigest, nonce);
            resultDigest.update((byte)':');

            md5(resultDigest, nc);
            resultDigest.update((byte)':');

            md5(resultDigest, cnonce);
            resultDigest.update((byte)':');

            md5(resultDigest, qop);
            resultDigest.update((byte)':');

            {
                scratchDigest.reset();

                md5(scratchDigest, requestMethod);
                scratchDigest.update((byte)':');

                md5(scratchDigest, uri);

                update(resultDigest, scratchDigest.digest());
            }

            appendHex(sb, resultDigest.digest());
        }
Ejemplo n.º 15
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public static void main(String[] paramArrayOfString) throws Exception
        public static void Main(string[] paramArrayOfString)
        {
            bool @bool;

            char[] arrayOfChar;
            char   c;
            string str1;

            if (paramArrayOfString.Length == 1 || paramArrayOfString.Length == 2)
            {
                string[] arrayOfString = paramArrayOfString[0].Split(":", true);
                str1 = arrayOfString[0];
                c    = (arrayOfString.Length == 1) ? (char)443 : (char)int.Parse(arrayOfString[1]);
                string str = (paramArrayOfString.Length == 1) ? "changeit" : paramArrayOfString[1];
                arrayOfChar = str.ToCharArray();
            }
            else
            {
                Console.WriteLine("Usage: java InstallCert [:port] [passphrase]");
                return;
            }
            File file = new File("jssecacerts");

            if (!file.File)
            {
                char c1    = Path.DirectorySeparatorChar;
                File file1 = new File(System.getProperty("java.home") + c1 + "lib" + c1 + "security");
                file = new File(file1, "jssecacerts");
                if (!file.File)
                {
                    file = new File(file1, "cacerts");
                }
            }
            Console.WriteLine("Loading KeyStore " + file + "...");
            FileStream fileInputStream = new FileStream(file, FileMode.Open, FileAccess.Read);
            KeyStore   keyStore        = KeyStore.getInstance(KeyStore.DefaultType);

            keyStore.load(fileInputStream, arrayOfChar);
            fileInputStream.Close();
            SSLContext          sSLContext          = SSLContext.getInstance("TLS");
            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.DefaultAlgorithm);

            trustManagerFactory.init(keyStore);
            X509TrustManager   x509TrustManager   = (X509TrustManager)trustManagerFactory.TrustManagers[0];
            SavingTrustManager savingTrustManager = new SavingTrustManager(x509TrustManager);

            sSLContext.init(null, new TrustManager[] { savingTrustManager }, null);
            SSLSocketFactory sSLSocketFactory = sSLContext.SocketFactory;

            Console.WriteLine("Opening connection to " + str1 + ":" + c + "...");
            SSLSocket sSLSocket = (SSLSocket)sSLSocketFactory.createSocket(str1, c);

            sSLSocket.SoTimeout = 10000;
            try
            {
                Console.WriteLine("Starting SSL handshake...");
                sSLSocket.startHandshake();
                sSLSocket.close();
                Console.WriteLine();
                Console.WriteLine("No errors, certificate is already trusted");
            }
            catch (SSLException sSLException)
            {
                Console.WriteLine();
                sSLException.printStackTrace(System.out);
            }
            X509Certificate[] arrayOfX509Certificate = savingTrustManager.chain;
            if (arrayOfX509Certificate == null)
            {
                Console.WriteLine("Could not obtain server certificate chain");
                return;
            }
            StreamReader bufferedReader = new StreamReader(System.in);

            Console.WriteLine();
            Console.WriteLine("Server sent " + arrayOfX509Certificate.Length + " certificate(s):");
            Console.WriteLine();
            MessageDigest messageDigest1;
            MessageDigest messageDigest2 = (messageDigest1 = MessageDigest.getInstance("SHA1")).getInstance("MD5");

            for (sbyte b = 0; b < arrayOfX509Certificate.Length; b++)
            {
                X509Certificate x509Certificate1 = arrayOfX509Certificate[b];
                Console.WriteLine(" " + (b + true) + " Subject " + x509Certificate1.SubjectDN);
                Console.WriteLine("   Issuer  " + x509Certificate1.IssuerDN);
                messageDigest1.update(x509Certificate1.Encoded);
                Console.WriteLine("   sha1    " + toHexString(messageDigest1.digest()));
                messageDigest2.update(x509Certificate1.Encoded);
                Console.WriteLine("   md5     " + toHexString(messageDigest2.digest()));
                Console.WriteLine();
            }
            Console.WriteLine("Enter certificate to add to trusted keystore or 'q' to quit: [1]");
            string str2 = bufferedReader.ReadLine().Trim();

            try
            {
                @bool = (str2.Length == 0) ? 0 : (int.Parse(str2) - 1);
            }
            catch (System.FormatException)
            {
                Console.WriteLine("KeyStore not changed");
                return;
            }
            X509Certificate x509Certificate = arrayOfX509Certificate[@bool];
            string          str3            = str1 + "-" + (@bool + true);

            keyStore.setCertificateEntry(str3, x509Certificate);
            FileStream fileOutputStream = new FileStream("jssecacerts", FileMode.Create, FileAccess.Write);

            keyStore.store(fileOutputStream, arrayOfChar);
            fileOutputStream.Close();
            Console.WriteLine();
            Console.WriteLine(x509Certificate);
            Console.WriteLine();
            Console.WriteLine("Added certificate to keystore 'jssecacerts' using alias '" + str3 + "'");
        }