Android Base64 Encode base64Digest(String input)

Here you can find the source of base64Digest(String input)

Description

First encrypt with SHA1 then convert to Base64

Declaration

public static String base64Digest(String input)
        throws UnsupportedEncodingException, NoSuchAlgorithmException 

Method Source Code

//package com.java2s;
import java.io.UnsupportedEncodingException;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.util.Base64;

public class Main {
    /** First encrypt with SHA1 then convert to Base64*/
    public static String base64Digest(String input)
            throws UnsupportedEncodingException, NoSuchAlgorithmException {

        String b64digest = convertToBase64(dataDigest(input));

        // we trim off last character due to a weird encoding error
        // Note: check for a solution in the future.
        return b64digest.substring(0, b64digest.length() - 1);
    }//ww  w .j  a v  a2  s.  co m

    private static String convertToBase64(byte[] in)
            throws UnsupportedEncodingException {
        if (in == null)
            return null;

        return new String(Base64.encode(in, Base64.URL_SAFE
                | Base64.NO_PADDING), "UTF8");
    }

    private static byte[] dataDigest(String in)
            throws NoSuchAlgorithmException, UnsupportedEncodingException {
        if (in == null)
            return null;

        MessageDigest md = MessageDigest.getInstance("SHA-1");
        return md.digest(in.getBytes("UTF8"));
    }
}

Related

  1. encode(String str)
  2. encodedBase64(String currentString)
  3. toBase64(String value)
  4. toBase64(byte[] bytes)
  5. base64Encode(byte[] bytes)
  6. base64Encode(final byte[] data)
  7. encode(byte[] src)
  8. to64(String a)
  9. toBase64(byte[] barray)