Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timob 5030: Android: Add support for HMAC SHA256 to mirror recent addition to iOS #618

Closed
wants to merge 12 commits into from
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiBlob;
import org.appcelerator.titanium.TiContext;
import org.appcelerator.titanium.util.TiConvert;

@Kroll.module
public class UtilsModule extends KrollModule
Expand All @@ -31,6 +30,15 @@ public UtilsModule()
{
super();
}

private String convertToString(Object obj) {
if (obj instanceof String)
return (String)obj;
else if (obj instanceof TiBlob)
return ((TiBlob)obj).getText();
else
throw new IllegalArgumentException("Invalid type for argument");
}

public UtilsModule(TiContext tiContext)
{
Expand All @@ -43,57 +51,68 @@ public TiBlob base64encode(Object obj)
if (obj instanceof TiBlob) {
return TiBlob.blobFromString(((TiBlob)obj).toBase64());
}
String data;
try {
if (obj instanceof byte[]) {
data = new String((byte[])obj, "UTF-8");
} else {
data = TiConvert.toString(obj);
String data = convertToString(obj);
if (data != null) {
try {
return TiBlob.blobFromString(new String(Base64.encodeBase64(data.getBytes("UTF-8")), "UTF-8"));
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "UTF-8 is not a supported encoding type");
}
return TiBlob.blobFromString(new String(Base64.encodeBase64(data.getBytes("UTF-8")), "UTF-8"));
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "UTF-8 is not a supported encoding type");
}
return null;
}

@Kroll.method
public TiBlob base64decode(String data)
public TiBlob base64decode(Object obj)
{
try {
return TiBlob.blobFromData(Base64.decodeBase64(data.getBytes("UTF-8")));
} catch (UnsupportedEncodingException e) {
String data = convertToString(obj);
if (data != null) {
try {
return TiBlob.blobFromData(Base64.decodeBase64(data.getBytes("UTF-8")));
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "UTF-8 is not a supported encoding type");
}
}

return null;
}

@Kroll.method
public String md5HexDigest(String data)
{
return DigestUtils.md5Hex(data);
public String md5HexDigest(Object obj) {
String data = convertToString(obj);
if (data != null)
return DigestUtils.md5Hex(data);
return null;
}

@Kroll.method
public String sha1(String data)
{
public String sha1(Object obj) {
String data = convertToString(obj);
if (data != null)
{
return DigestUtils.shaHex(data);
}
return null;
}

@Kroll.method
public String sha256(Object obj) {
String data = convertToString(obj);
//NOTE: DigestUtils with the version before 1.4 doesn't have the function sha256Hex,
//so we deal with it ourselves
try
{
byte[] b = data.getBytes();
MessageDigest algorithm = MessageDigest.getInstance("SHA-1");
MessageDigest algorithm = MessageDigest.getInstance("SHA-256");
algorithm.reset();
algorithm.update(b);
byte messageDigest[] = algorithm.digest();
StringBuilder result = new StringBuilder();
//NOTE: for some reason DigestUtils doesn't produce correct value
//so we deal with it ourselves
for (int i=0; i < messageDigest.length; i++) {
result.append(Integer.toString(( messageDigest[i] & 0xff ) + 0x100, 16).substring(1));
}
return result.toString();
} catch(NoSuchAlgorithmException e) {
Log.e(TAG, "SHA1 is not a supported algorithm");
Log.e(TAG, "SHA256 is not a supported algorithm");
}
return null;
}
Expand Down
36 changes: 17 additions & 19 deletions apidoc/Titanium/Utils/Utils.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,42 @@ methods:
- name: base64decode
description: decode a Base64 string
returns:
type: String
type: TiBlob
parameters:
- name: str
description: the string to use for the input
type: String
- name: obj
description: the string or TiBlob to use for the input
type: Object
- name: base64encode
description: encode a string into Base64
returns:
type: String
type: TiBlob
parameters:
- name: str
description: the string to use for the input
type: String
- name: obj
description: the string or TiBlob to use for the input
type: Object
- name: md5HexDigest
description: compute a MD5 hash algorithm against the input and return a hex-based string
returns:
type: String
parameters:
- name: str
description: the string to use for the input
type: String
- name: obj
description: the string or TiBlob to use for the input
type: Object
- name: sha1
since: "1.3.2"
description: compute a SHA-1 hash algorithm against the input and return a hex-based string
returns:
type: String
parameters:
- name: str
description: the string to use for the input
type: String
- name: obj
description: the string or TiBlob to use for the input
type: Object
- name: sha256
since: "1.8.0"
platforms: [iphone, ipad]
description: compute a SHA-256 hash algorithm against the input and return a hex-based string
returns:
type: String
parameters:
- name: str
description: the string to use for the input
type: String

- name: obj
description: the string or TiBlob to use for the input
type: Object