Skip to content

Commit

Permalink
Re-enabled crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
qhng committed Nov 14, 2017
1 parent 5fc60b1 commit d9a80d2
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 304 deletions.
44 changes: 5 additions & 39 deletions hooks/after_prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,7 @@ module.exports = function(context) {
console.log('encrypt: ' + file);
});

if (platform == 'ios') {
var pluginDir;
try {
var ios_parser = context.requireCordovaModule('cordova-lib/src/cordova/metadata/ios_parser'),
iosParser = new ios_parser(platformPath);
pluginDir = path.join(iosParser.cordovaproj, 'Plugins', context.opts.plugin.id);
} catch (err) {
var xcodeproj_dir = fs.readdirSync(platformPath).filter(function(e) { return e.match(/\.xcodeproj$/i); })[0],
xcodeproj = path.join(platformPath, xcodeproj_dir),
originalName = xcodeproj.substring(xcodeproj.lastIndexOf(path.sep)+1, xcodeproj.indexOf('.xcodeproj')),
cordovaproj = path.join(platformPath, originalName);

pluginDir = path.join(cordovaproj, 'Plugins', context.opts.plugin.id);
}
replaceCryptKey_ios(pluginDir, key, iv);

} else if (platform == 'android') {
if (platform == 'android') {
var pluginDir = path.join(platformPath, 'src');
replaceCryptKey_android(pluginDir, key, iv);

Expand Down Expand Up @@ -129,32 +113,14 @@ module.exports = function(context) {
}

function encryptData(input, key, iv) {
//var cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
//var encrypted = cipher.update(input, 'utf8', 'base64') + cipher.final('base64');
var cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
var encrypted = cipher.update(input, 'utf8', 'base64') + cipher.final('base64');

//return encrypted;
return new Buffer(input).toString("base64");
}

function replaceCryptKey_ios(pluginDir, key, iv) {
var sourceFile = path.join(pluginDir, 'CDVCryptURLProtocol.m');
var content = fs.readFileSync(sourceFile, 'utf-8');

var includeArrStr = targetFiles.include.map(function(pattern) { return '@"' + pattern.replace('\\', '\\\\') + '"'; }).join(', ');
var excludeArrStr = targetFiles.exclude.map(function(pattern) { return '@"' + pattern.replace('\\', '\\\\') + '"'; }).join(', ');

content = content.replace(/kCryptKey = @".*";/, 'kCryptKey = @"' + key + '";')
.replace(/kCryptIv = @".*";/, 'kCryptIv = @"' + iv + '";')
.replace(/kIncludeFiles\[\] = {.*};/, 'kIncludeFiles\[\] = { ' + includeArrStr + ' };')
.replace(/kExcludeFiles\[\] = {.*};/, 'kExcludeFiles\[\] = { ' + excludeArrStr + ' };')
.replace(/kIncludeFileLength = [0-9]+;/, 'kIncludeFileLength = ' + targetFiles.include.length + ';')
.replace(/kExcludeFileLength = [0-9]+;/, 'kExcludeFileLength = ' + targetFiles.exclude.length + ';');

fs.writeFileSync(sourceFile, content, 'utf-8');
return encrypted;
}

function replaceCryptKey_android(pluginDir, key, iv) {
var sourceFile = path.join(pluginDir, 'com/qhng/cordova/DecryptResource.java');
var sourceFile = path.join(pluginDir, 'com/qhng/cordova/DecryptResourceNG.java');
var content = fs.readFileSync(sourceFile, 'utf-8');

var includeArrStr = targetFiles.include.map(function(pattern) { return '"' + pattern.replace('\\', '\\\\') + '"'; }).join(', ');
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
},
"keywords": [
"ecosystem:cordova",
"cordova-android",
"cordova-ios"
"cordova-android"
],
"author": "tkyaji, qhng",
"license": "Apache version 2.0"
Expand Down
20 changes: 3 additions & 17 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,15 @@
<author>@tkyaji, @qhng</author>
<license>Apache 2.0 License</license>

<platform name="ios">
<config-file target="config.xml" parent="/*">
<feature name="CDVCrypt">
<param name="ios-package" value="CDVCrypt"/>
<param name="onload" value="true"/>
</feature>
</config-file>

<header-file src="src/ios/CDVCrypt.h" />
<source-file src="src/ios/CDVCrypt.m" />
<header-file src="src/ios/CDVCryptURLProtocol.h" />
<source-file src="src/ios/CDVCryptURLProtocol.m" />
</platform>

<platform name="android">
<config-file target="config.xml" parent="/*">
<feature name="DecryptResource">
<param name="android-package" value="com.qhng.cordova.DecryptResource" />
<feature name="DecryptResourceNG">
<param name="android-package" value="com.qhng.cordova.DecryptResourceNG" />
<param name="onload" value="true" />
</feature>
</config-file>

<source-file src="src/android/com/qhng/cordova/DecryptResource.java" target-dir="src/com/qhng/cordova" />
<source-file src="src/android/com/qhng/cordova/DecryptResourceNG.java" target-dir="src/com/qhng/cordova" />
</platform>

<cryptfiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import javax.crypto.spec.SecretKeySpec;


public class DecryptResource extends CordovaPlugin {
public class DecryptResourceNG extends CordovaPlugin {

private static final String TAG = "DecryptResource";
private static final String TAG = "DecryptResourceNG";

private static final String CRYPT_KEY = "";
private static final String CRYPT_IV = "";
Expand Down Expand Up @@ -62,13 +62,12 @@ public CordovaResourceApi.OpenForReadResult handleOpenForRead(Uri uri) throws IO
LOG.d(TAG, "decrypt: " + uriStr);
ByteArrayInputStream byteInputStream = null;
try {
//SecretKey skey = new SecretKeySpec(CRYPT_KEY.getBytes("UTF-8"), "AES");
//cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//cipher.init(Cipher.DECRYPT_MODE, skey, new IvParameterSpec(CRYPT_IV.getBytes("UTF-8")));
SecretKey skey = new SecretKeySpec(CRYPT_KEY.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skey, new IvParameterSpec(CRYPT_IV.getBytes("UTF-8")));

ByteArrayOutputStream bos = new ByteArrayOutputStream();
//bos.write(cipher.doFinal(bytes));
bos.write(bytes);
bos.write(cipher.doFinal(bytes));
byteInputStream = new ByteArrayInputStream(bos.toByteArray());

} catch (Exception ex) {
Expand Down
15 changes: 0 additions & 15 deletions src/ios/CDVCrypt.h

This file was deleted.

19 changes: 0 additions & 19 deletions src/ios/CDVCrypt.m

This file was deleted.

13 changes: 0 additions & 13 deletions src/ios/CDVCryptURLProtocol.h

This file was deleted.

192 changes: 0 additions & 192 deletions src/ios/CDVCryptURLProtocol.m

This file was deleted.

0 comments on commit d9a80d2

Please sign in to comment.