Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert byte array to Base64 String in android

So I have the user enter password to signup from the android app.

Before I save the password to the database on the server I want to convert it to a MD5 one way hash and save it to the database.

MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e3) {
        // TODO Auto-generated catch block
        e3.printStackTrace();
    }
    try {
        md.update(password.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e3) {
        // TODO Auto-generated catch block
        e3.printStackTrace();
    }
    byte raw[] = md.digest();

How do I convert this byte array to a Base64 string. I saw in some forum that android util package left out the Base64 encoding and decoding on the other hand I see the encodetoString function in the android developers site.

Any help is appreciated.

like image 231
Aakash Avatar asked Feb 26 '23 17:02

Aakash


1 Answers

For such a basic utility class, as there is a late implementation of Base64 in API 8, I would copy the latest source from the AOSP git repo to my own application package. It compiles with API level 3.

This way, you can code in your application with the same API as the official android Base64 implementation and stay compatible with all android versions. The day API 8 becomes the oldest version of android on the market, you just have to delete your version of the class in your package and update the imports to the official android.util.Base64.

Eclipse hint: just follow the link I provided to the source in your browser, Ctrl-A to select all the code, Ctrl-C to copy it, open Eclipse, click on the package where you want to create the class, hit Ctrl-V. The .java file is created automatically with the source directly modified with the correct package declaration.

like image 134
Kevin Gaudin Avatar answered Feb 28 '23 05:02

Kevin Gaudin