Ejemplo n.º 1
0
        public virtual int sceMd5BlockResult(TPointer contextAddr, TPointer resultAddr)
        {
            sbyte[] result = md5.digest();
            writeMd5Digest(resultAddr.Address, result);

            return(0);
        }
        public Value mhash(Env env, int hash, StringValue data, @Optional string key)
        {
            if (key != null)
            {
                throw new UnsupportedOperationException("key");
            }

            MhashAlgorithm algorithm = _algorithmMap.get(hash);

            if (algorithm == null)
            {
                return(BooleanValue.FALSE);
            }

            MessageDigest messageDigest = algorithm.createMessageDigest();

            if (messageDigest == null)
            {
                log.warning(L.l("no MessageDigest for {0}", algorithm));

                return(BooleanValue.FALSE);
            }

            byte[] result = messageDigest.digest(data.toBytes());

            return(env.createBinaryBuilder(result));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Calculates the hash sum of the stream. The method must be
        /// called after the stream is completely consumed.
        /// </summary>
        /// <returns> the hash sum </returns>
        /// <exception cref="IllegalStateException"> if the stream is not consumed completely,
        /// completely means that hasNext() returns false </exception>
        public virtual BigInteger calculateHashSum()
        {
            //    if (hasNext())
            //      throw new IllegalStateException("stream must be consumed completely!");

            return(new BigInteger(digest.digest()));
        }
Ejemplo n.º 4
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.º 5
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);
        }
Ejemplo n.º 6
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;
            }
        }
Ejemplo n.º 7
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.º 8
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.º 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 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.º 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
            protected internal static int digest(TPointer inAddr, int inSize, TPointer outAddr, string algorithm, int hashLength)
            {
                sbyte[] input = inAddr.getArray8(inSize);
                sbyte[] hash  = null;
                try
                {
                    MessageDigest md = MessageDigest.getInstance(algorithm);
                    hash = md.digest(input);
                }
                catch (Exception e)
                {
                    // Ignore...
                    Console.WriteLine(string.Format("SceKernelUtilsContext({0}).digest", algorithm), e);
                }
                if (hash != null)
                {
                    outAddr.setArray(hash, hashLength);
                }

                return(0);
            }
Ejemplo n.º 14
0
            public virtual int result(TPointer ctxAddr, TPointer resultAddr)
            {
                sbyte[] hash = null;
                if (input != null)
                {
                    try
                    {
                        MessageDigest md = MessageDigest.getInstance(algorithm);
                        hash = md.digest(input);
                    }
                    catch (Exception e)
                    {
                        // Ignore...
                        Console.WriteLine(string.Format("SceKernelUtilsContext({0}).result", algorithm), e);
                    }
                }

                if (hash != null)
                {
                    resultAddr.setArray(hash, hashLength);
                }

                return(0);
            }
        /**
         * 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.º 16
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 + "'");
        }