Skip to content

Commit

Permalink
Merge pull request #8 from hansemannn/update-ios
Browse files Browse the repository at this point in the history
Update project to latest environment
  • Loading branch information
hansemannn authored Aug 15, 2018
2 parents a113643 + a53965a commit 383d25a
Show file tree
Hide file tree
Showing 33 changed files with 1,137 additions and 1,240 deletions.
26 changes: 26 additions & 0 deletions android/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
Language: Java
AccessModifierOffset: -4
AllowShortBlocksOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
# class, constructor, method should be next line
BreakBeforeBraces: Linux
# Keep '=' at end of line when wrapping, but move things like '&&', '||' to beginning of newline
BreakBeforeBinaryOperators: NonAssignment
# FIXME: break for brace after synchronized block, anonymous class declarations
BreakAfterJavaFieldAnnotations: true
ColumnLimit: 120
IndentCaseLabels: true
IndentWidth: 4
MaxEmptyLinesToKeep: 1
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpacesInParentheses: false
TabWidth: 4
UseTab: ForContinuationAndIndentation
SpaceAfterCStyleCast: true
# Spaces inside {} for array literals, i.e. "new Object[] { args }"
Cpp11BracedListStyle: false
ReflowComments: false
27 changes: 18 additions & 9 deletions android/src/ti/crypto/CryptoModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,29 @@
import org.appcelerator.kroll.common.Log;

@Kroll.module(name = "Crypto", id = "ti.crypto")
public class CryptoModule extends KrollModule {
public class CryptoModule extends KrollModule
{
private static final String LCAT = "CryptoModule";

static {
static
{
Security.addProvider(new org.spongycastle.jce.provider.BouncyCastleProvider());
}

public CryptoModule() {
public CryptoModule()
{
super();
}

// Methods
@Kroll.method
public int encodeData(@SuppressWarnings("rawtypes") HashMap args) {
public int encodeData(@SuppressWarnings("rawtypes") HashMap args)
{
// Validate and grab the arguments we need.
if (args == null || !args.containsKey("type") || !args.containsKey("source") || !args.containsKey("dest")) {
Log.e(LCAT, "Not all required parameters and keys provided to encodeData! Please check the documentation and your usage.");
Log.e(
LCAT,
"Not all required parameters and keys provided to encodeData! Please check the documentation and your usage.");
return STATUS_PARAMERROR;
}
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -69,7 +75,8 @@ public int encodeData(@SuppressWarnings("rawtypes") HashMap args) {
// Verify that the offset is within range.
int destLength = dest.getLength();
if (destPosition >= destLength) {
Log.e(LCAT, "Destination position of " + destPosition + " is past end of buffer. Buffer size is " + destLength + ".");
Log.e(LCAT, "Destination position of " + destPosition + " is past end of buffer. Buffer size is "
+ destLength + ".");
return STATUS_BUFFERTOOSMALL;
}

Expand All @@ -87,10 +94,13 @@ public int encodeData(@SuppressWarnings("rawtypes") HashMap args) {
}

@Kroll.method
public String decodeData(@SuppressWarnings("rawtypes") HashMap args) {
public String decodeData(@SuppressWarnings("rawtypes") HashMap args)
{
// Validate and grab the arguments we need.
if (args == null || !args.containsKey("type") || !args.containsKey("source")) {
Log.e(LCAT, "Not all required parameters and keys provided to decodeData! Please check the documentation and your usage.");
Log.e(
LCAT,
"Not all required parameters and keys provided to decodeData! Please check the documentation and your usage.");
return null;
}
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -201,5 +211,4 @@ public String decodeData(@SuppressWarnings("rawtypes") HashMap args) {
public static final String TYPE_HEXSTRING = "hexstring";
@Kroll.constant
public static final String TYPE_BASE64STRING = "base64string";

}
71 changes: 47 additions & 24 deletions android/src/ti/crypto/CryptorProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@
import ti.modules.titanium.BufferProxy;
import android.util.Log;

@Kroll.proxy(creatableInModule = CryptoModule.class, propertyAccessors = { "resizeBuffer", "operation", "algorithm", "options", "key",
"initializationVector" })
public class CryptorProxy extends KrollProxy {
@Kroll.proxy(creatableInModule = CryptoModule.class,
propertyAccessors = { "resizeBuffer", "operation", "algorithm", "options", "key", "initializationVector" })
public class CryptorProxy extends KrollProxy
{

private static final String LCAT = "CryptoModule";
private PaddedBufferedBlockCipher _encryptCipher;
private PaddedBufferedBlockCipher _decryptCipher;

private class CryptOptions {
private class CryptOptions
{
// Configuration Options
public boolean resizeBuffer;
public int operation;
Expand All @@ -48,24 +50,30 @@ private class CryptOptions {
public BufferProxy dataOut;
public int dataOutLength;

public byte[] getBytesIn() {
public byte[] getBytesIn()
{
return dataIn.getBuffer();
}

public int getDataInLength() {
public int getDataInLength()
{
return dataInLength > 0 ? dataInLength : dataIn.getLength();
}

public int getDataOutLength() {
public int getDataOutLength()
{
return dataOutLength > 0 ? dataOutLength : dataOut.getLength();
}

public boolean isEncrypt() {
public boolean isEncrypt()
{
return operation == CryptoModule.ENCRYPT;
}
}

private CryptOptions prepareCryptOptions(BufferProxy dataIn, int dataInLength, BufferProxy dataOut, int dataOutLength) {
private CryptOptions prepareCryptOptions(BufferProxy dataIn, int dataInLength, BufferProxy dataOut,
int dataOutLength)
{
CryptOptions o = new CryptOptions();

KrollDict dict = this.getProperties();
Expand All @@ -74,7 +82,8 @@ private CryptOptions prepareCryptOptions(BufferProxy dataIn, int dataInLength, B
o.algorithm = dict.optInt("algorithm", CryptoModule.ALGORITHM_AES128);
o.options = dict.optInt("options", 0);
o.key = dict.containsKey("key") ? ((BufferProxy) dict.get("key")).getBuffer() : null;
o.iv = dict.containsKey("initializationVector") ? ((BufferProxy) dict.get("initializationVector")).getBuffer() : null;
o.iv = dict.containsKey("initializationVector") ? ((BufferProxy) dict.get("initializationVector")).getBuffer()
: null;

o.dataIn = dataIn;
o.dataInLength = dataInLength > 0 || dataIn == null ? dataInLength : dataIn.getLength();
Expand All @@ -84,7 +93,8 @@ private CryptOptions prepareCryptOptions(BufferProxy dataIn, int dataInLength, B
return o;
}

private void logBytes(String id, byte[] arr) {
private void logBytes(String id, byte[] arr)
{
StringBuffer out = new StringBuffer();
if (arr == null) {
out.append("{{ null }}");
Expand All @@ -100,7 +110,8 @@ private void logBytes(String id, byte[] arr) {
Log.i(LCAT, id + ": " + out.toString());
}

private PaddedBufferedBlockCipher createCipher(CryptOptions o) {
private PaddedBufferedBlockCipher createCipher(CryptOptions o)
{
// Start off by figuring out what engine to use.
// (This will influence if we can use a block cipher or a stream cipher.)
BlockCipher bEngine;
Expand Down Expand Up @@ -166,7 +177,8 @@ private PaddedBufferedBlockCipher createCipher(CryptOptions o) {
return cipher;
}

private PaddedBufferedBlockCipher getCipher(CryptOptions o) {
private PaddedBufferedBlockCipher getCipher(CryptOptions o)
{
if (o.isEncrypt()) {
if (_encryptCipher == null)
_encryptCipher = createCipher(o);
Expand All @@ -178,7 +190,8 @@ private PaddedBufferedBlockCipher getCipher(CryptOptions o) {
}
}

private int crypt(CryptOptions o) throws Exception {
private int crypt(CryptOptions o) throws Exception
{
// Get our cipher.
PaddedBufferedBlockCipher cipher = getCipher(o);
if (cipher == null)
Expand All @@ -200,8 +213,10 @@ private int crypt(CryptOptions o) throws Exception {
}

@Kroll.method
public int encrypt(BufferProxy dataIn, @Kroll.argument(optional = true) int dataInLength, @Kroll.argument(optional = true) BufferProxy dataOut,
@Kroll.argument(optional = true) int dataOutLength) {
public int encrypt(BufferProxy dataIn, @Kroll.argument(optional = true) int dataInLength,
@Kroll.argument(optional = true) BufferProxy dataOut,
@Kroll.argument(optional = true) int dataOutLength)
{
try {
CryptOptions o = prepareCryptOptions(dataIn, dataInLength, dataOut, dataOutLength);
o.operation = CryptoModule.ENCRYPT;
Expand All @@ -215,8 +230,10 @@ public int encrypt(BufferProxy dataIn, @Kroll.argument(optional = true) int data
}

@Kroll.method
public int decrypt(BufferProxy dataIn, @Kroll.argument(optional = true) int dataInLength, @Kroll.argument(optional = true) BufferProxy dataOut,
@Kroll.argument(optional = true) int dataOutLength) {
public int decrypt(BufferProxy dataIn, @Kroll.argument(optional = true) int dataInLength,
@Kroll.argument(optional = true) BufferProxy dataOut,
@Kroll.argument(optional = true) int dataOutLength)
{
try {
CryptOptions o = prepareCryptOptions(dataIn, dataInLength, dataOut, dataOutLength);
o.operation = CryptoModule.DECRYPT;
Expand All @@ -230,7 +247,8 @@ public int decrypt(BufferProxy dataIn, @Kroll.argument(optional = true) int data
}

@Kroll.method
public int getOutputLength(int dataInLength, boolean isFinal) {
public int getOutputLength(int dataInLength, boolean isFinal)
{
if (dataInLength < 0) {
dataInLength = 0;
}
Expand All @@ -240,8 +258,10 @@ public int getOutputLength(int dataInLength, boolean isFinal) {
}

@Kroll.method
public int update(BufferProxy dataIn, @Kroll.argument(optional = true) int dataInLength, @Kroll.argument(optional = true) BufferProxy dataOut,
@Kroll.argument(optional = true) int dataOutLength) {
public int update(BufferProxy dataIn, @Kroll.argument(optional = true) int dataInLength,
@Kroll.argument(optional = true) BufferProxy dataOut,
@Kroll.argument(optional = true) int dataOutLength)
{
try {
// Get our cipher.
CryptOptions o = prepareCryptOptions(dataIn, dataInLength, dataOut, dataOutLength);
Expand All @@ -268,7 +288,8 @@ public int update(BufferProxy dataIn, @Kroll.argument(optional = true) int dataI
}

@Kroll.method
public int finish(BufferProxy dataOut, @Kroll.argument(optional = true) int dataOutLength) {
public int finish(BufferProxy dataOut, @Kroll.argument(optional = true) int dataOutLength)
{
try {
// Get our cipher.
CryptOptions o = prepareCryptOptions(null, 0, dataOut, dataOutLength);
Expand All @@ -294,7 +315,8 @@ public int finish(BufferProxy dataOut, @Kroll.argument(optional = true) int data
}

@Kroll.method
public int reset(@Kroll.argument(optional = true) BufferProxy initializationBuffer) {
public int reset(@Kroll.argument(optional = true) BufferProxy initializationBuffer)
{
if (initializationBuffer != null) {
setProperty("initializationVector", initializationBuffer);
}
Expand All @@ -304,7 +326,8 @@ public int reset(@Kroll.argument(optional = true) BufferProxy initializationBuff
}

@Kroll.method(name = "release")
public int _release() {
public int _release()
{
_encryptCipher = null;
_decryptCipher = null;
return CryptoModule.STATUS_SUCCESS;
Expand Down
12 changes: 8 additions & 4 deletions android/src/ti/crypto/utility/Hex.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

package ti.crypto.utility;

public class Hex {
public class Hex
{

/**
* Prevent instantiation.
*/
private Hex() {
private Hex()
{
}

/**
Expand All @@ -21,7 +23,8 @@ private Hex() {
* A hex string to convert.
* @return The byte array.
*/
public static byte[] convertFromHex(String s) {
public static byte[] convertFromHex(String s)
{
// Source: http://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java
int len = s.length();
byte[] data = new byte[len / 2];
Expand All @@ -38,7 +41,8 @@ public static byte[] convertFromHex(String s) {
* A byte array to convert.
* @return The hex string.
*/
public static String convertToHex(byte[] data) {
public static String convertToHex(byte[] data)
{
StringBuffer result = new StringBuffer();
for (int i = 0; i < data.length; i++) {
result.append(Integer.toHexString(data[i]));
Expand Down
Loading

0 comments on commit 383d25a

Please sign in to comment.