Package java.security

Examples of java.security.MessageDigest


   * @return      HASH结果
   */
  private static byte[] digest(String type, byte[] bytes)
  {
        try {
         MessageDigest dist = MessageDigest.getInstance(type);
        return dist.digest(bytes);
        } catch (NoSuchAlgorithmException e) {
        throw new IllegalArgumentException("Cannot find digest:"+type, e);
        }
  }
View Full Code Here


         return f.toString();
      }

      protected static byte[] internalCreateHash(String pathName) throws NoSuchAlgorithmException
      {
         MessageDigest digest = getDigest();
         try
         {
            // update
            digest.update(pathName.getBytes());
            // return
            return digest.digest();
         }
         finally
         {
            // reset
            digest.reset();
         }
      }
View Full Code Here

  /**
   * Creates a md5 encoded in base64 for given input.
   */
  public static String md5(byte[] input) {
    try {
      MessageDigest md5 = MessageDigest.getInstance("MD5");
      md5.reset();
      md5.update(input);
      return jfix.util.Base64.encodeBytes(md5.digest());
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
View Full Code Here

   * @return MD5 encrypted string
   */
  public static String encrypt(String s) {
    byte[] inbytes = s.getBytes();
    try {
      MessageDigest md5Helper = MessageDigest.getInstance(HASH_ALGORITHM);
      byte[] outbytes = md5Helper.digest(inbytes);
      String out = md5Encoder.encode(outbytes);
      return out;
    } catch (NoSuchAlgorithmException e) {
      throw new AssertException("Cannot load MD5 Message Digest ," + HASH_ALGORITHM + " not supported");
    }
View Full Code Here

        }
        return buf.toString();
    }

    public static String encodeSha1(String text) {
        MessageDigest md;
        try {
            md = MessageDigest.getInstance("SHA-1");
        } catch (NoSuchAlgorithmException e) {
            try {
                md = MessageDigest.getInstance("MD5");
            } catch (NoSuchAlgorithmException e1) {
                throw new RuntimeException(e1);
            }
        }

        byte[] sha1hash = new byte[40];

        md.update(text.getBytes(), 0, text.length());

        sha1hash = md.digest();

        return convertToHex(sha1hash);
    }
View Full Code Here

    return res.toArray(new BaseAuthorization[res.size()]);
  }

  private String encode(String plaintext) {
    try {
      MessageDigest md = MessageDigest.getInstance("MD5");
      md.update(plaintext.getBytes("UTF-8"));
      return Base64.encodeBytes(md.digest());
    } catch (NoSuchAlgorithmException e) {
      log.error(e.getMessage(), e);
    } catch (UnsupportedEncodingException e) {
      log.error(e.getMessage(), e);
    }
View Full Code Here

      }
      String username = args[0];
      String realm = args[1];
      String password = args[2];
      String A1 = username + ":" + realm + ":" + password;
      MessageDigest digest = MessageDigest.getInstance("MD5");
      String hA1 = H(A1, digest);
      System.out.println("RFC2617 A1 hash: " + hA1);
   }
View Full Code Here

    */
   protected synchronized MessageDigest getDigest()
   {
      if (this.digest == null)
      {
         MessageDigest d = null;
         try
         {
            d = MessageDigest.getInstance(algorithm);
         }
         catch (NoSuchAlgorithmException e)
View Full Code Here

      if( log.isTraceEnabled() )
         log.trace("H(N) xor H(g): "+CryptoUtil.tob64(hxg));
      clientHash.update(hxg);
      if( log.isTraceEnabled() )
      {
         MessageDigest tmp = CryptoUtil.copy(clientHash);
         log.trace("H[H(N) xor H(g)]: "+CryptoUtil.tob64(tmp.digest()));
      }
      // clientHash = H(N) xor H(g) | H(U)
      clientHash.update(CryptoUtil.newDigest().digest(username.getBytes()));
      if( log.isTraceEnabled() )
      {
         MessageDigest tmp = CryptoUtil.copy(clientHash);
         log.trace("H[H(N) xor H(g) | H(U)]: "+CryptoUtil.tob64(tmp.digest()));
      }
      // clientHash = H(N) xor H(g) | H(U) | s
      clientHash.update(params.s);
      if( log.isTraceEnabled() )
      {
         MessageDigest tmp = CryptoUtil.copy(clientHash);
         log.trace("H[H(N) xor H(g) | H(U) | s]: "+CryptoUtil.tob64(tmp.digest()));
      }
      K = null;
   }
View Full Code Here

      byte[] nb = CryptoUtil.trim(B.toByteArray());
      // clientHash = H(N) xor H(g) | H(U) | s | A
      clientHash.update(ab);
      if( log.isTraceEnabled() )
      {
         MessageDigest tmp = CryptoUtil.copy(clientHash);
         log.trace("H[H(N) xor H(g) | H(U) | s | A]: "+CryptoUtil.tob64(tmp.digest()));
      }
      // clientHash = H(N) xor H(g) | H(U) | A | B
      clientHash.update(nb);
      if( log.isTraceEnabled() )
      {
         MessageDigest tmp = CryptoUtil.copy(clientHash);
         log.trace("H[H(N) xor H(g) | H(U) | s | A | B]: "+CryptoUtil.tob64(tmp.digest()));
      }
      // serverHash = A
      serverHash.update(ab);
      // Calculate u as the first 32 bits of H(B)
      byte[] hB = CryptoUtil.newDigest().digest(nb);
      byte[] ub =
      {hB[0], hB[1], hB[2], hB[3]};
      // Calculate S = (A * v^u) ^ b % N
      BigInteger A = new BigInteger(1, ab);
      if( log.isTraceEnabled() )
         log.trace("A: "+CryptoUtil.tob64(A.toByteArray()));
      if( log.isTraceEnabled() )
         log.trace("B: "+CryptoUtil.tob64(B.toByteArray()));
      if( log.isTraceEnabled() )
         log.trace("v: "+CryptoUtil.tob64(v.toByteArray()));
      BigInteger u = new BigInteger(1, ub);
      if( log.isTraceEnabled() )
         log.trace("u: "+CryptoUtil.tob64(u.toByteArray()));
      BigInteger A_v2u = A.multiply(v.modPow(u, N)).mod(N);
      if( log.isTraceEnabled() )
         log.trace("A * v^u: "+CryptoUtil.tob64(A_v2u.toByteArray()));
      BigInteger S = A_v2u.modPow(b, N);
      if( log.isTraceEnabled() )
         log.trace("S: "+CryptoUtil.tob64(S.toByteArray()));
      // K = SessionHash(S)
      MessageDigest sessionDigest = MessageDigest.getInstance(params.hashAlgorithm);
      K = sessionDigest.digest(S.toByteArray());
      if( log.isTraceEnabled() )
         log.trace("K: "+CryptoUtil.tob64(K));
      // clientHash = H(N) xor H(g) | H(U) | A | B | K
      clientHash.update(K);
      if( log.isTraceEnabled() )
      {
         MessageDigest tmp = CryptoUtil.copy(clientHash);
         log.trace("H[H(N) xor H(g) | H(U) | s | A | B | K]: "+CryptoUtil.tob64(tmp.digest()));
      }
   }
View Full Code Here

TOP

Related Classes of java.security.MessageDigest

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.