Skip to content

Commit

Permalink
update demo
Browse files Browse the repository at this point in the history
  • Loading branch information
OctMon committed Apr 3, 2019
1 parent 5db7e5a commit 9964423
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 70 deletions.
1 change: 1 addition & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

165 changes: 121 additions & 44 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 18 additions & 18 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ class MyApp extends StatefulWidget {
}

class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
final _string =
"Java, android, ios, get the same result by DES encryption and decryption.";
final _key = "u1BvOHzUOcklgNpn1MaWvdn9DT4LyzSX";
final _iv = "12345678";
String _encrypt = '';
String _decrypt = '';

@override
void initState() {
Expand All @@ -22,33 +27,28 @@ class _MyAppState extends State<MyApp> {

// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
platformVersion = await FlutterDes.platformVersion;
} on PlatformException {
platformVersion = 'Failed to get platform version.';
_encrypt = await FlutterDes.encryptToHex(_string, _key, iv: _iv);
_decrypt = await FlutterDes.decryptFromHex(_encrypt, _key, iv: _iv);
setState(() {});
} catch (e) {
print(e);
}

// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;

setState(() {
_platformVersion = platformVersion;
});
}

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
title: const Text('DES example app'),
),
body: Center(
child: Text('Running on: $_platformVersion\n'),
body: Padding(
padding: const EdgeInsets.all(15.0),
child: Center(
child: Text(
'string = $_string\nkey = $_key\niv = $_iv\nencrypt = $_encrypt\ndecrypt = $_decrypt'),
),
),
),
);
Expand Down
1 change: 1 addition & 0 deletions flutter_des.iml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<excludeFolder url="file://$MODULE_DIR$/example/.dart_tool" />
<excludeFolder url="file://$MODULE_DIR$/example/.pub" />
<excludeFolder url="file://$MODULE_DIR$/example/build" />
<excludeFolder url="file://$MODULE_DIR$/example/ios/Flutter/App.framework/flutter_assets/packages" />
</content>
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Dart SDK" level="project" />
Expand Down
18 changes: 12 additions & 6 deletions ios/Classes/SwiftFlutterDesPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class SwiftFlutterDesPlugin: NSObject, FlutterPlugin {
return
}
let string = arguments[0] as? String ?? ""
let key = arguments[0] as? String ?? ""
let iv = arguments[0] as? String ?? ""
let key = arguments[1] as? String ?? ""
let iv = arguments[2] as? String ?? ""
switch call.method {
case "encrypt":
encrypt(string: string, key: key, iv: iv, result: result)
Expand All @@ -31,13 +31,19 @@ public class SwiftFlutterDesPlugin: NSObject, FlutterPlugin {
}

func encrypt(string: String, key: String, iv: String, result: FlutterResult) {
let data = string.data(using: .utf8)?.crypt(operation: CCOperation(kCCEncrypt), key: key, iv: iv)
result(data?.toHexString ?? nil)
guard let data = string.data(using: .utf8)?.crypt(operation: CCOperation(kCCEncrypt), key: key, iv: iv) else {
result(nil)
return
}
result(data.toHexString)
}

func decrypt(string: String, key: String, iv: String, result: FlutterResult) {
let data = string.hexToData.crypt(operation: CCOperation(kCCDecrypt), key: key, iv: iv)
result(data ?? nil)
guard let data = string.hexToData.crypt(operation: CCOperation(kCCDecrypt), key: key, iv: iv) else {
result(nil)
return
}
result(String(data: data, encoding: .utf8) ?? nil)
}

}
Expand Down
Loading

0 comments on commit 9964423

Please sign in to comment.