illab.nabal.util.SecurityHelper.java Source code

Java tutorial

Introduction

Here is the source code for illab.nabal.util.SecurityHelper.java

Source

/*
 * Copyright (C) 2013-2014 Tan Jung
 *
 * 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.
 */

package illab.nabal.util;

import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.http.protocol.HTTP;

import android.util.Base64;

/**
 * Security helper.
 * 
 * @version 1.0, 02/10/14
 * @author <a href="mailto:tanito.jung@gmail.com">Tan Jung</a>
 */
public class SecurityHelper {

    /**
     * Constant for AES.
     */
    private final static String AES = "AES";

    /**
     * Constant for HMAC-SHA1.
     */
    private final static String HMAC_SHA1 = "HMAC-SHA1";

    /**
     * Secret key spec for system AES encryption / decryption.
     */
    private final static SecretKeySpec SYSTEM_SECRET_KEY_SPEC = new SecretKeySpec(
            Base64.decode("X2hpbWl0c3Vfbm9fa2FnaQ==", Base64.DEFAULT), AES);

    /**
      * AES-encrypt a plain message. Return null if message is empty.
      * 
      * @param message
      * @return AES encrypted hex
      * @throws Exception
      */
    public static String cipher(String message) throws Exception {
        if (StringHelper.isEmpty(message) == false) {
            Cipher cipher = Cipher.getInstance(AES);
            cipher.init(Cipher.ENCRYPT_MODE, SYSTEM_SECRET_KEY_SPEC);
            return Base64.encodeToString(cipher.doFinal(message.getBytes()), Base64.DEFAULT);
        } else {
            return null;
        }
    }

    /**
     * Decrypt a AES-encrypted message.
     * 
     * @param encrypted
     * @return String
     * @throws Exception
     */
    public static String decipher(String encrypted) throws Exception {
        if (StringHelper.isEmpty(encrypted) == false) {
            Cipher cipher = Cipher.getInstance(AES);
            cipher.init(Cipher.DECRYPT_MODE, SYSTEM_SECRET_KEY_SPEC);
            return new String(cipher.doFinal(Base64.decode(encrypted, Base64.DEFAULT)), HTTP.UTF_8);
        } else {
            return null;
        }
    }

    /**
     * Get HMACSHA1-encoded OAuth 1.0a signature string.
     * 
     * @param secretKey - secret key to encode basestring with
     * @param baseString - signature base string 
     * @return oauthSignature - HMAC-SHA1 encoded signature string
     * @throws Exception
     */
    public static String getHmacSha1Signature(String secretKey, String baseString) throws Exception {
        String oauthSignature = null;

        // #################### IMPORTANT ####################
        // the secret key is the concatenated values (each first encoded per Parameter 
        // Encoding) of the Consumer Secret and Token Secret, separated by an '&' character 
        // (ASCII code 38) even if empty.

        if (StringHelper.isAllFull(secretKey, baseString) == true) {
            byte[] keyBytes = secretKey.getBytes(HTTP.UTF_8);
            SecretKey keySpec = new SecretKeySpec(keyBytes, HMAC_SHA1);
            Mac mac = Mac.getInstance(HMAC_SHA1);
            mac.init(keySpec);
            oauthSignature = new String(Base64.encode(mac.doFinal(baseString.getBytes(HTTP.UTF_8)), Base64.DEFAULT),
                    HTTP.UTF_8).trim();
        }
        return oauthSignature;
    }

}