mirror of
https://github.com/purplecabbage/phonegap-plugins.git
synced 2026-04-24 03:00:11 -04:00
115
Android/Hmac/HmacPlugin.java
Normal file
115
Android/Hmac/HmacPlugin.java
Normal file
@@ -0,0 +1,115 @@
|
||||
/* Copyright (c) 2011 - Zitec COM
|
||||
*
|
||||
* @author: Ionut Voda <ionut.voda@zitec.ro>
|
||||
*/
|
||||
package com.phonegap.plugin.hmac;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.util.Base64;
|
||||
import android.util.Log;
|
||||
|
||||
import com.phonegap.api.Plugin;
|
||||
import com.phonegap.api.PluginResult;
|
||||
import com.phonegap.api.PluginResult.Status;
|
||||
|
||||
/**
|
||||
* @author Ionut Voda <ionut.voda@zitec.ro>
|
||||
* @category Plugin
|
||||
*/
|
||||
public class HmacPlugin extends Plugin {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see com.phonegap.api.Plugin#execute(java.lang.String, org.json.JSONArray, java.lang.String)
|
||||
* @see http://download.oracle.com/javase/1.4.2/docs/guide/security/jce/JCERefGuide.html#AppA for
|
||||
* a complete Mac class reference
|
||||
*
|
||||
* @param String hashingMethod indicates the hashing method: sha1, md5, etc
|
||||
* @param JSONArray hashParams contains the string to be hashed and the hash key
|
||||
* @param String callback
|
||||
* @return PluginResult
|
||||
*/
|
||||
@Override
|
||||
public PluginResult execute(String hashingMethod, JSONArray hashParams, String callbackId) {
|
||||
// intialize internal vars
|
||||
String stringToHash = null;
|
||||
String hashKey = null;
|
||||
PluginResult result = null;
|
||||
String hashedString = null;
|
||||
|
||||
// read the params
|
||||
try {
|
||||
stringToHash = hashParams.getString(0);
|
||||
hashKey = hashParams.getString(1);
|
||||
} catch (JSONException jsonEx) {
|
||||
Log.d("HmacPlugin", "JSON Exception " + jsonEx.getMessage());
|
||||
result = new PluginResult(Status.JSON_EXCEPTION);
|
||||
}
|
||||
|
||||
// hash the string
|
||||
try {
|
||||
hashedString = hasher(hashingMethod, stringToHash, hashKey);
|
||||
} catch (InvalidKeyException e) {
|
||||
Log.d("HmacPlugin", "InvalidKeyException " + e.getMessage());
|
||||
result = new PluginResult(Status.ILLEGAL_ACCESS_EXCEPTION);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
Log.d("HmacPlugin", "UnsupportedEncodingException " + e.getMessage());
|
||||
result = new PluginResult(Status.ILLEGAL_ACCESS_EXCEPTION);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
Log.d("HmacPlugin", "NoSuchAlgorithmException " + e.getMessage());
|
||||
result = new PluginResult(Status.ILLEGAL_ACCESS_EXCEPTION);
|
||||
}
|
||||
|
||||
// prepare the response as JSON
|
||||
if (hashedString != null) {
|
||||
JSONObject JSONresult = new JSONObject();
|
||||
try {
|
||||
JSONresult.put("hash", hashedString);
|
||||
} catch (JSONException jsonEx) {
|
||||
Log.d("HmacPlugin", "JSON Exception " + jsonEx.getMessage());
|
||||
result = new PluginResult(Status.JSON_EXCEPTION);
|
||||
}
|
||||
result = new PluginResult(Status.OK, JSONresult);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method in charge with generating the actual hash
|
||||
*
|
||||
* @param String hashingMethod md5/sha1
|
||||
* @param String stringToHash
|
||||
* @param String hashKey
|
||||
* @return String
|
||||
*/
|
||||
protected String hasher(String hashingMethod, String stringToHash, String hashKey)
|
||||
throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
|
||||
|
||||
String keyGenerator = "HmacMD5";
|
||||
if(hashingMethod.equals(new String("sha1"))) {
|
||||
keyGenerator = "HmacSHA1";
|
||||
}
|
||||
else if (hashingMethod.equals(new String("md5"))) {
|
||||
keyGenerator = "HmacMD5";
|
||||
}
|
||||
|
||||
SecretKeySpec key = new SecretKeySpec((hashKey).getBytes("UTF-8"), keyGenerator);
|
||||
Mac hmac = Mac.getInstance(keyGenerator);
|
||||
hmac.init(key);
|
||||
|
||||
byte[] bytes = hmac.doFinal(stringToHash.getBytes("UTF-8"));
|
||||
return new String(Base64.encode(bytes, Base64.NO_WRAP));
|
||||
}
|
||||
|
||||
}
|
||||
26
Android/Hmac/README.md
Normal file
26
Android/Hmac/README.md
Normal file
@@ -0,0 +1,26 @@
|
||||
/* Copyright (c) 2011 - Zitec COM
|
||||
*
|
||||
* @author: Ionut Voda <ionut.voda@zitec.ro>
|
||||
*/
|
||||
|
||||
(1)The current plugin will help you produce UTF-8 compatible HMAC hashes(http://en.wikipedia.org/wiki/HMAC). It currently supports 2 hasher functions: sha1 which produces a HMAC-SHA1 hash and md5 whic generates a HMAC-MD5.
|
||||
|
||||
(2)How to use it:
|
||||
// to generated a HMAC-SHA1 hash
|
||||
window.plugins.hmac.sha1(
|
||||
"string you want to hash",
|
||||
"key to hash with",
|
||||
function hashResult(jsonHash) {
|
||||
//the param has a single property called "hash" that contains your hash
|
||||
alert(jsonHash.hash)
|
||||
}
|
||||
);
|
||||
|
||||
// or the MD5 version
|
||||
window.plugins.hmac.md5(
|
||||
"string you want to hash",
|
||||
"key to hash with",
|
||||
function hashResult(jsonHash) {
|
||||
alert(jsonHash.hash)
|
||||
}
|
||||
);
|
||||
49
Android/Hmac/hmac.js
Normal file
49
Android/Hmac/hmac.js
Normal file
@@ -0,0 +1,49 @@
|
||||
/* Copyright (c) 2011 - Zitec COM
|
||||
*
|
||||
* @author: Ionut Voda <ionut.voda@zitec.ro>
|
||||
*/
|
||||
|
||||
/**
|
||||
* @return the Hmac class instance
|
||||
*/
|
||||
var Hmac = function() {}
|
||||
|
||||
/**
|
||||
* Define the sha1 function under HMAC class
|
||||
*
|
||||
* @param stringToHash The string to be hashed
|
||||
* @param hashKey The hash key
|
||||
* @param successCallback The callback which will be called when directory listing is successful
|
||||
* @param failureCallback The callback which will be called when directory listing encouters an error
|
||||
*/
|
||||
Hmac.prototype.sha1 = function(stringToHash, hashKey, successCallback, failureCallback) {
|
||||
return PhoneGap.exec(successCallback, failureCallback, 'HmacPlugin',
|
||||
'sha1', [stringToHash, hashKey]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the md5 function under HMAC class
|
||||
*
|
||||
* @param stringToHash The string to be hashed
|
||||
* @param hashKey The hash key
|
||||
* @param successCallback The callback which will be called when directory listing is successful
|
||||
* @param failureCallback The callback which will be called when directory listing encouters an error
|
||||
*/
|
||||
Hmac.prototype.md5 = function(stringToHash, hashKey, successCallback, failureCallback) {
|
||||
return PhoneGap.exec(successCallback, failureCallback, 'HmacPlugin',
|
||||
'md5', [stringToHash, hashKey]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the Directory Listing Javascript plugin
|
||||
* Also register native call which will be called when this plugin runs
|
||||
*/
|
||||
PhoneGap.addConstructor(function() {
|
||||
//Register the javascript plugin with PhoneGap
|
||||
PhoneGap.addPlugin('hmac', new Hmac());
|
||||
|
||||
//Register the native class of plugin with PhoneGap
|
||||
PluginManager.addService("HmacPlugin", "com.phonegap.plugin.hmac.HmacPlugin");
|
||||
});
|
||||
Reference in New Issue
Block a user