Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.security.MessageDigest;

import android.util.Base64;
import android.util.Log;

public class Main {
    public static String createPubNubSafeBase64Hash(String input) {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(input.getBytes());
            String encodedChannelName = Base64.encodeToString(messageDigest.digest(), Base64.URL_SAFE);

            //pubnub channel names cannot be more than 92 characters
            if (encodedChannelName.length() > 92) {
                encodedChannelName = encodedChannelName.substring(0, 91);
            }
            //pubnub channel names cannot have whitespace characters
            return encodedChannelName.trim();
        } catch (Exception e) {
            Log.d("X", "Error in encoding: " + e.getMessage());
            return null;
        }
    }
}