Java Checksum Calculate generateCheckSum(String string)

Here you can find the source of generateCheckSum(String string)

Description

This method generates a checksum from a passed string

License

Open Source License

Parameter

Parameter Description
string to generate checksum

Return

checksum string

Declaration

public static String generateCheckSum(String string) 

Method Source Code

//package com.java2s;
/**//from ww w  .j  a  va  2s  . c  o  m
 * Copyright (c) 2016 Dynamicloud
 * <p/>
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * <p/>
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * <p/>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * <p/>
 * <code>WiztorageUtils</code>
 * <p/>
 * This class provides a set of generic utilities
 *
 * @author Eleazar Gomez
 */

import java.security.MessageDigest;

public class Main {
    /**
     * This method generates a checksum from a passed string
     *
     * @param string to generate checksum
     * @return checksum string
     */
    public static String generateCheckSum(String string) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(string.getBytes());

            byte byteData[] = md.digest();

            StringBuilder hexString = new StringBuilder();
            for (byte aByteData : byteData) {
                String hex = Integer.toHexString(0xff & aByteData);
                if (hex.length() == 1)
                    hexString.append('0');
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (java.security.NoSuchAlgorithmException missing) {
            return "";
        }
    }
}

Related

  1. checksumLength(int algorithmCode, int dataLength)
  2. createChecksum(File file, String shaAlgo)
  3. generateChecksum(InputStream is)
  4. generateChecksum(String path, String flavor)
  5. generateChecksum(String pType, String pInFile)
  6. getChecksum(byte[] buffer, int offset, int length)
  7. getChecksum(final File file)
  8. getCheckSum(String filePath)
  9. getChecksumAlder32(File file)