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.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Security » SHA 




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.