Hash string : Hash Code « Development Class « Java






Hash string

     
//    Openbravo POS is a point of sales application designed for touch screens.
//    Copyright (C) 2007-2009 Openbravo, S.L.
//    http://www.openbravo.com/product/pos
//
//    This file is part of Openbravo POS.
//
//    Openbravo POS is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Openbravo POS is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Openbravo POS.  If not, see <http://www.gnu.org/licenses/>.

//package com.openbravo.pos.pda.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 
 * @author jaroslawwozniak
 */
public class StringUtils {

  private static final char[] hexchars = { '0', '1', '2', '3', '4', '5', '6',
      '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

  public static String byte2hex(byte[] binput) {

    StringBuffer sb = new StringBuffer(binput.length * 2);
    for (int i = 0; i < binput.length; i++) {
      int high = ((binput[i] & 0xF0) >> 4);
      int low = (binput[i] & 0x0F);
      sb.append(hexchars[high]);
      sb.append(hexchars[low]);
    }
    return sb.toString();
  }

  public static byte[] hex2byte(String sinput) {
    int length = sinput.length();

    if ((length & 0x01) != 0) {
      throw new IllegalArgumentException("odd number of characters.");
    }

    byte[] out = new byte[length >> 1];

    // two characters form the hex value.
    for (int i = 0, j = 0; j < length; i++) {
      int f = Character.digit(sinput.charAt(j++), 16) << 4;
      f = f | Character.digit(sinput.charAt(j++), 16);
      out[i] = (byte) (f & 0xFF);
    }

    return out;
  }

  public static String hashString(String sPassword) {

    if (sPassword == null || sPassword.equals("")) {
      return "empty:";
    } else {
      try {
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.update(sPassword.getBytes("UTF-8"));
        byte[] res = md.digest();
        return "sha1:" + byte2hex(res);
      } catch (NoSuchAlgorithmException e) {
        return "plain:" + sPassword;
      } catch (UnsupportedEncodingException e) {
        return "plain:" + sPassword;
      }
    }
  }
}

   
    
    
    
    
  








Related examples in the same category

1.Computing hash codes
2.A hash-code generator and a collection of static hash-code generation methods.
3.MD5 hash generator
4.Hash 32 String
5.Hash 64 String
6.MD5 hashing: Encodes a string
7.MD5 String
8.Hash Code BuilderHash Code Builder
9.HashCode generationHashCode generation
10.Get hash code for primitive data types
11.Return as hash code for the given object
12.Null Safe Hash Code
13.A very efficient java hash algorithm, based on the BuzHash algoritm
14.Easy implementation of hashCode
15.An implementation of the HMACT64 keyed hashing algorithm
16.Gets the hash code of an object returning zero when the object is null
17.Unify Hash
18.Secure Hash
19.FNV Hash
20.Jenkins Hash
21.Concurrent Cuckoo hashing using lock striping. Uses R/W locks for resizing. Exercise solution.
22.Concurrent Cuckoo hashing using lock striping.
23.encode Hex
24.Fowler/Noll/Vo hash algorhythm
25.Produces 32-bit hash for hash table lookup. (Jenkins Hash Function)
26.Key Value Hash
27.Paul Hsieh's Hash Function.
28.An extension of WeakReference that implements a sane equals and hashcode method.
29.Dual Key Hash Map
30.A hash map with int key and int values.
31.null Safe Equals and Hash
32.Generates a hash code for a given source code.
33.AtkinsonHash utility class implements the hash algorithm used by HyperCard's ask password command.
34.Hash Code AssistHash Code Assist
35.This is a very fast, non-cryptographic hash suitable for general hash-based lookup.
36.An advanced hash table supporting configurable garbage collection semantics of keys and values