Java Digest digest(byte[] bytes, String txt)

Here you can find the source of digest(byte[] bytes, String txt)

Description

Compute a MD5 hash from the text.

License

Open Source License

Declaration

public final static byte[] digest(byte[] bytes, String txt) 

Method Source Code

//package com.java2s;
/* //from   w ww .  j ava  2  s.co  m
 * Copyright (C) 2009 by ?yvind Hanssen (ohanssen@acm.org)
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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.
 */

import java.security.*;

public class Main {
    /**
     * Compute a MD5 hash from the text. Text can be given as
     * an array of bytes, a string or both. A string will be converted
     * to bytes using the UTF-8 encoding before computing the hash.
     */
    public final static byte[] digest(byte[] bytes, String txt) {
        try {
            MessageDigest dig = MessageDigest.getInstance("MD5");
            if (bytes != null)
                dig.update(bytes);
            if (txt != null)
                dig.update(txt.getBytes("UTF-8"));

            return dig.digest();
        } catch (Exception e) {
            System.out.println("*** Cannot generate message digest: " + e);
            return null;
        }
    }
}

Related

  1. digest(byte[] buffer)
  2. digest(byte[] bytes)
  3. digest(byte[] bytes, String algorithm)
  4. digest(byte[] bytes, String algorithmName)
  5. digest(byte[] data)
  6. digest(byte[] data, String algorithm)
  7. digest(byte[] data, String algorithm)
  8. digest(byte[] input)