SHA1 Sum : SHA « Security « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Security » SHAScreenshots 
SHA1 Sum
    
/**
 * This file is distributed under the GPL
 * $Id: SHA1Sum.java 1810 2009-06-22 17:33:07Z scotta $
 */

//package net.bnubot.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;

//import net.bnubot.logging.Out;

/**
 @author scotta
 */
public class SHA1Sum {
  private final byte[] sha1sum;
  private String display = null;

  public SHA1Sum(String hexStrthrows Exception {
    if(!hexStr.matches("[0-9a-fA-F]{40}"))
      throw new Exception("Invalid format: " + hexStr);
    display = hexStr.toLowerCase();
    sha1sum = new byte[20];
    for(int i = 0; i < 20; i++) {
      int pos = i << 1;
      sha1sum[i(byteInteger.parseInt(hexStr.substring(pos, pos+2)16);
    }
  }

  public SHA1Sum(byte[] bytesthrows Exception {
    MessageDigest digest = MessageDigest.getInstance("SHA1");
    digest.update(bytes, 0, bytes.length);
    sha1sum = digest.digest();
  }

  public SHA1Sum(File fthrows Exception {
    MessageDigest digest = MessageDigest.getInstance("SHA1");
    InputStream is = new FileInputStream(f);
    byte[] buffer = new byte[8192];
    do {
      int read = is.read(buffer);
      if(read <= 0)
        break;
      digest.update(buffer, 0, read);
    while(true);
    sha1sum = digest.digest();

  
  }

  private static String hexChr(int b) {
    return Integer.toHexString(b & 0xF);
  }

  private static String toHex(int b) {
    return hexChr((b & 0xF0>> 4+ hexChr(b & 0x0F);
  }

  @Override
  public String toString() {
    if(display == null) {
      display = "";
      for(byte b : sha1sum)
        display += toHex(b);
    }
    return display;
  }

  public byte[] getSum() {
    return sha1sum;
  }

  @Override
  public boolean equals(Object obj) {
    if(!(obj instanceof SHA1Sum))
      return false;

    byte[] obj_sha1sum = ((SHA1Sum)obj).sha1sum;
    if(sha1sum.length != obj_sha1sum.length)
      return false;

    for(int i = 0; i < sha1sum.length; i++)
      if(sha1sum[i!= obj_sha1sum[i])
        return false;

    return true;
  }
}

   
    
    
    
  
Related examples in the same category
1.An alternate SHA Interleave algorithm as implemented in the SRP distribution
2.SHA_Interleave algorithm as described in section 3.1 of RFC2945
3.Get Sha 1 string
4.Returns the Jensen-Shannon divergence.
5.Encrypt password by using SHA-256 algorithm, encryptedPassword length is 32 bits
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.