Java Hex Convert To fromHexDigest(String hexDigest)

Here you can find the source of fromHexDigest(String hexDigest)

Description

from Hex Digest

License

Open Source License

Declaration

public static byte[] fromHexDigest(String hexDigest) 

Method Source Code

//package com.java2s;
/*//w  ww .  ja va 2  s.c om
 * ====================================================================
 * Copyright (c) 2004-2010 TMate Software Ltd.  All rights reserved.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.  The terms
 * are also available at http://svnkit.com/license.html
 * If newer versions of this license are posted there, you may use a
 * newer version instead, at your option.
 * ====================================================================
 */

public class Main {
    public static byte[] fromHexDigest(String hexDigest) {
        if (hexDigest == null || hexDigest.length() == 0) {
            return null;
        }

        hexDigest = hexDigest.toLowerCase();

        int digestLength = hexDigest.length() / 2;

        if (digestLength == 0 || 2 * digestLength != hexDigest.length()) {
            return null;
        }

        byte[] digest = new byte[digestLength];
        for (int i = 0; i < hexDigest.length() / 2; i++) {
            if (!isHex(hexDigest.charAt(2 * i)) || !isHex(hexDigest.charAt(2 * i + 1))) {
                return null;
            }

            int hi = Character.digit(hexDigest.charAt(2 * i), 16) << 4;

            int lo = Character.digit(hexDigest.charAt(2 * i + 1), 16);
            Integer ib = new Integer(hi | lo);
            byte b = ib.byteValue();

            digest[i] = b;
        }

        return digest;
    }

    private static boolean isHex(char ch) {
        return Character.isDigit(ch) || (Character.toUpperCase(ch) >= 'A' && Character.toUpperCase(ch) <= 'F');
    }
}

Related

  1. fromHex8B(String src)
  2. fromHexa(String s)
  3. fromHexBinary(String s)
  4. fromHexChar(char ch)
  5. fromHexChars(char[] chars, int i)
  6. fromHexDigit(final char c)
  7. fromHexDigit(int c)
  8. fromHexNibble(char n)
  9. fromHexNibble(final char n)