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

cordova10 #96

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# Cordova crypt file plugin
HTML source file is encrypted at build, and decrypted at run.
https://www.npmjs.com/package/cordova-plugin-crypt-file
# Cordova crypt file plugin NextGen
This is an extension to [tkyaji's cordova-plugin-crypt-file](https://github.com/tkyaji/cordova-plugin-crypt-file) implementation to encrypt HTML assets during build and to decrypt the required assets during runtime.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please do not change original text!


The original implementation can also be found on https://www.npmjs.com/package/cordova-plugin-crypt-file.

## Requires node-rsa
`npm install -g node-rsa`

## Add Plugin
`cordova plugin add cordova-plugin-crypt-file`
`cordova plugin add https://github.com/qhng/cordova-plugin-crypt-file`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not change original link


## Encrypt
`cordova build [ios / android]`
Expand Down Expand Up @@ -43,9 +47,7 @@ Specify the target file as a regular expression.


## Supported platforms
* iOS
* Android
* CrossWalk

## Before reporting your issue
It would be very helpful if you show me your project (If you have GitHub repository, that URL would be nice).
Expand Down
64 changes: 23 additions & 41 deletions hooks/after_prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,29 @@ module.exports = function(context) {
platforms = context.requireCordovaModule('cordova-lib/src/platforms/platforms'),
Parser = context.requireCordovaModule('cordova-lib/src/cordova/metadata/parser'),
ParserHelper = context.requireCordovaModule('cordova-lib/src/cordova/metadata/parserhelper/ParserHelper'),
ConfigParser = context.requireCordovaModule('cordova-common').ConfigParser;
ConfigParser = context.requireCordovaModule('cordova-common').ConfigParser,
NodeRSA = context.requireCordovaModule('node-rsa');

var deferral = new Q.defer();
var projectRoot = cordova_util.cdProjectRoot();

var keypair = new NodeRSA();
keypair.generateKeyPair(1024);
var key = crypto.randomBytes(24).toString('base64');
var iv = crypto.randomBytes(12).toString('base64');

console.log('key=' + key + ', iv=' + iv)
var publicKey = keypair.exportKey("pkcs8-public");
console.log(publicKey);
console.log(keypair.exportKey("pkcs8-private"));
publicKey = publicKey.replace("-----BEGIN PUBLIC KEY-----", "");
publicKey = publicKey.replace("-----END PUBLIC KEY-----", "");
var publicKeyHexa = new Buffer(publicKey).toString("hex");
publicKeyHexa = publicKeyHexa.replace(/0a/g, "");
publicKey = new Buffer(publicKeyHexa, "hex").toString("ascii");
var encryptedKey = keypair.encryptPrivate(key, "base64");
var encryptedIv = keypair.encryptPrivate(iv, "base64");
console.log('key(P)=' + key + ', iv(P)=' + iv)
console.log('key(E)=' + encryptedKey + ', iv(E)=' + encryptedIv)

var targetFiles = loadCryptFileTargets();

Expand All @@ -38,25 +52,9 @@ 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);
replaceCryptKey_android(pluginDir, encryptedKey, encryptedIv, publicKey);

var cfg = new ConfigParser(platformInfo.projectConfig.path);
cfg.doc.getroot().getchildren().filter(function(child, idx, arr) {
Expand Down Expand Up @@ -135,32 +133,16 @@ module.exports = function(context) {
return encrypted;
}

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');
}

function replaceCryptKey_android(pluginDir, key, iv) {
var sourceFile = path.join(pluginDir, 'com/tkyaji/cordova/DecryptResource.java');
function replaceCryptKey_android(pluginDir, encryptedKey, encryptedIv, publicPem) {
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(', ');
var excludeArrStr = targetFiles.exclude.map(function(pattern) { return '"' + pattern.replace('\\', '\\\\') + '"'; }).join(', ');

content = content.replace(/CRYPT_KEY = ".*";/, 'CRYPT_KEY = "' + key + '";')
.replace(/CRYPT_IV = ".*";/, 'CRYPT_IV = "' + iv + '";')
content = content.replace(/_CRYPT_KEY = ".*";/, '_CRYPT_KEY = "' + encryptedKey + '";')
.replace(/_CRYPT_IV = ".*";/, '_CRYPT_IV = "' + encryptedIv + '";')
.replace(/PUBLIC_PEM = ".*";/, 'PUBLIC_PEM = "' + publicPem + '";')
.replace(/INCLUDE_FILES = new String\[\] {.*};/, 'INCLUDE_FILES = new String[] { ' + includeArrStr + ' };')
.replace(/EXCLUDE_FILES = new String\[\] {.*};/, 'EXCLUDE_FILES = new String[] { ' + excludeArrStr + ' };');

Expand Down
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
{
"name": "cordova-plugin-crypt-file",
"version": "1.3.3",
"name": "cordova-plugin-crypt-file-ng",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not change original files

"version": "0.1.0",
"description": "This plugin to encrypt/decrypt the source files.",
"repository": {
"type": "git",
"url": "https://github.com/tkyaji/cordova-plugin-crypt-file.git"
"url": "https://github.com/qhng/cordova-plugin-crypt-file.git"
},
"keywords": [
"ecosystem:cordova",
"cordova-android",
"cordova-ios"
"cordova-android"
],
"author": "tkyaji",
"dependencies": {
"node-rsa": "0.4.2"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that node-rsa better as node-forge...

},
"author": "tkyaji, qhng",
"license": "Apache version 2.0"
}
26 changes: 6 additions & 20 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -1,36 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
id="cordova-plugin-crypt-file"
version="1.3.3">
id="cordova-plugin-crypt-file-ng"
version="0.1.0">

<name>CordovaCrypt</name>
<description>Plugin Description</description>
<author>@tkyaji</author>
<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.tkyaji.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/tkyaji/cordova/DecryptResource.java" target-dir="src/com/tkyaji/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
@@ -1,4 +1,4 @@
package com.tkyaji.cordova;
package com.qhng.cordova;

import android.net.Uri;
import android.util.Base64;
Expand All @@ -19,15 +19,30 @@
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;

public class DecryptResource extends CordovaPlugin {

private static final String TAG = "DecryptResource";
public class DecryptResourceNG extends CordovaPlugin {

private static final String CRYPT_KEY = "";
private static final String CRYPT_IV = "";
private static final String TAG = "DecryptResourceNG";

private static final String PUBLIC_PEM = "";
private static final String _CRYPT_KEY = "";
private static final String _CRYPT_IV = "";
private static final String[] INCLUDE_FILES = new String[] { };
private static final String[] EXCLUDE_FILES = new String[] { };
private final String CRYPT_KEY;
private final String CRYPT_IV;

public DecryptResourceNG() throws Exception {
PublicKey pubKey = PublicKeyReader.get(PUBLIC_PEM);
Cipher rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding");
rsa.init(Cipher.DECRYPT_MODE, pubKey);
CRYPT_KEY = new String(rsa.doFinal(Base64.decode(_CRYPT_KEY, Base64.DEFAULT)));
CRYPT_IV = new String(rsa.doFinal(Base64.decode(_CRYPT_IV, Base64.DEFAULT)));
}

@Override
public Uri remapUri(Uri uri) {
Expand Down Expand Up @@ -98,3 +113,12 @@ private boolean hasMatch(String text, String[] regexArr) {
return false;
}
}

class PublicKeyReader {
public static PublicKey get(String publicPemStr) throws Exception {
byte[] keyBytes = Base64.decode(publicPemStr, Base64.DEFAULT);
X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePublic(spec);
}
}
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.

Loading