Android Open Source - qrcode-android Utils






From Project

Back to project page qrcode-android.

License

The source code is released under:

MIT License

If you think the Android project qrcode-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package tw.soleil.util;
/*ww w .  j a  v  a2  s .c om*/
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Formatter;
import java.util.List;
import java.util.Locale;
import java.util.Map;

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

import tw.soleil.constant.Constants;
import android.util.Base64;

public class Utils {
  public static String createSignature(Map<String, String> queryMap) {
    String orderedQueryStr = composeStrForSignatureEncryption(queryMap);
    byte[] base64Encoded = null;
    String signature = null;
    try {
      base64Encoded = Base64.encode(calculateRFC2104HMAC(orderedQueryStr, Constants.INVOICE_API_KEY).getBytes(Constants.CHARSET_FOR_INVOICE_API_ENCODING), Base64.DEFAULT);
      signature = new String(base64Encoded, Constants.CHARSET_FOR_INVOICE_API_ENCODING);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    return signature;
  }
  
  private static String composeStrForSignatureEncryption(Map<String, String> queryMap) {
    List<String> keySet = new ArrayList<String>(queryMap.keySet());
    Collections.sort(keySet, new Comparator<String>() {

      /**
       * compare string case insensitively
       */
      @Override
      public int compare(String str1, String str2) {
        return str1.toLowerCase(Locale.ENGLISH).compareTo(str2.toLowerCase(Locale.ENGLISH));
      }
    });
    List <String> orderedKeySet = keySet;
    StringBuffer sb = new StringBuffer();
    for (String key : orderedKeySet) {
      sb.append(key)
        .append("=")
        .append(queryMap.get(key))
        .append("&");
    }
    sb.deleteCharAt(sb.length() - 1); // delete last '&'
    
    return sb.toString();
  }
  
  private static String toHexString(byte[] bytes) {
    Formatter formatter = new Formatter();
    
    for (byte b : bytes) {
      formatter.format("%02x", b);
    }
    String hexStr = formatter.toString();
    formatter.close();
 
    return hexStr;
  }
 
  private static String calculateRFC2104HMAC(String data, String key)
    throws SignatureException, NoSuchAlgorithmException, InvalidKeyException
  {
    try {
      SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(Constants.CHARSET_FOR_INVOICE_API_ENCODING), Constants.HMAC_SHA1_ALGORITHM);
      Mac mac = Mac.getInstance(Constants.HMAC_SHA1_ALGORITHM);
      mac.init(signingKey);
      return toHexString(mac.doFinal(data.getBytes(Constants.CHARSET_FOR_INVOICE_API_ENCODING)));
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
}




Java Source Code List

com.google.zxing.client.android.BeepManager.java
com.google.zxing.client.android.CaptureActivityHandler.java
com.google.zxing.client.android.CaptureActivity.java
com.google.zxing.client.android.Contents.java
com.google.zxing.client.android.DecodeFormatManager.java
com.google.zxing.client.android.DecodeHandler.java
com.google.zxing.client.android.DecodeHintManager.java
com.google.zxing.client.android.DecodeThread.java
com.google.zxing.client.android.FinishListener.java
com.google.zxing.client.android.InactivityTimer.java
com.google.zxing.client.android.IntentSource.java
com.google.zxing.client.android.Intents.java
com.google.zxing.client.android.LocaleManager.java
com.google.zxing.client.android.ViewfinderResultPointCallback.java
com.google.zxing.client.android.ViewfinderView.java
com.google.zxing.client.android.camera.AutoFocusManager.java
com.google.zxing.client.android.camera.CameraConfigurationManager.java
com.google.zxing.client.android.camera.CameraManager.java
com.google.zxing.client.android.camera.FrontLightMode.java
com.google.zxing.client.android.camera.PreviewCallback.java
com.google.zxing.client.android.camera.exposure.DefaultExposureInterface.java
com.google.zxing.client.android.camera.exposure.ExposureInterface.java
com.google.zxing.client.android.camera.exposure.ExposureManager.java
com.google.zxing.client.android.camera.exposure.FroyoExposureInterface.java
com.google.zxing.client.android.camera.open.DefaultOpenCameraInterface.java
com.google.zxing.client.android.camera.open.GingerbreadOpenCameraInterface.java
com.google.zxing.client.android.camera.open.OpenCameraInterface.java
com.google.zxing.client.android.camera.open.OpenCameraManager.java
com.google.zxing.client.android.common.PlatformSupportManager.java
com.google.zxing.client.android.common.executor.AsyncTaskExecInterface.java
com.google.zxing.client.android.common.executor.AsyncTaskExecManager.java
com.google.zxing.client.android.common.executor.DefaultAsyncTaskExecInterface.java
com.google.zxing.client.android.common.executor.HoneycombAsyncTaskExecInterface.java
com.google.zxing.extra.RGBLuminanceSource.java
eu.livotov.zxscan.AutofocusMode.java
eu.livotov.zxscan.ZXScanHelper.java
eu.livotov.zxscan.ZXUserCallback.java
tw.soleil.constant.Constants.java
tw.soleil.qrcodereadertest.QRCodeReaderTestActivity.java
tw.soleil.service.InvoiceService.java
tw.soleil.to.InvoiceDtl.java
tw.soleil.to.Invoice.java
tw.soleil.util.DateUtil.java
tw.soleil.util.Utils.java