Java Digest digest(String ha1, String ha2, String nonce)

Here you can find the source of digest(String ha1, String ha2, String nonce)

Description

digest

License

Apache License

Declaration

public static String digest(String ha1, String ha2, String nonce) 

Method Source Code

//package com.java2s;
/*/*from  w  w  w.j ava 2s.co m*/
 * Digital Audio Access Protocol (DAAP) Library
 * Copyright (C) 2004-2010 Roger Kapsi
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

public class Main {
    /** ISO Latin 1 encoding */
    public static final String ISO_8859_1 = "ISO-8859-1";
    /** 0, 1, ... F */
    private static final char[] HEX = { '0', '1', '2', '3', '4', '5', '6',
            '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

    public static String digest(String ha1, String ha2, String nonce) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");

            md.update(getBytes(ha1, ISO_8859_1));
            md.update((byte) ':');
            md.update(getBytes(nonce, ISO_8859_1));
            md.update((byte) ':');
            md.update(getBytes(ha2, ISO_8859_1));
            return toHexString(md.digest());
        } catch (NoSuchAlgorithmException err) {
            // should never happen
            throw new RuntimeException(err);
        }
    }

    /**
     * String to byte Array
     */
    public static byte[] getBytes(String s, String charsetName) {
        try {
            return s.getBytes(charsetName);
        } catch (UnsupportedEncodingException e) {
            // should never happen
            throw new RuntimeException(e);
        }
    }

    /**
     * Returns b as hex String
     */
    public static String toHexString(byte[] b) {
        if (b.length % 2 != 0) {
            throw new IllegalArgumentException(
                    "Argument's length must be power of 2");
        }

        StringBuffer buffer = new StringBuffer(b.length * 2);
        for (int i = 0; i < b.length; i++) {
            char hi = HEX[((b[i] >> 4) & 0xF)];
            char lo = HEX[b[i] & 0xF];

            buffer.append(hi).append(lo);
        }
        return buffer.toString();
    }

    /**
     * Byte Array to String
     */
    public static String toString(byte[] b, String charsetName) {
        try {
            return new String(b, charsetName);
        } catch (UnsupportedEncodingException e) {
            // should never happen
            throw new RuntimeException(e);
        }
    }
}

Related

  1. digest(String algorithm, byte[] data)
  2. digest(String algorithm, String data)
  3. digest(String aValue)
  4. digest(String base)
  5. digest(String digest, byte[] data)
  6. digest(String input, Charset charset)
  7. digest(String input, String algorithm, String encoding)
  8. digest(String key)
  9. digest(String message, byte[] salt)