Skip to content

Commit

Permalink
Merge pull request jaumard#7 from pavankumarkatakam/master
Browse files Browse the repository at this point in the history
Feature for getting the app signature at runtime.
  • Loading branch information
jaumard committed Aug 2, 2019
2 parents d6c095a + c542a1a commit 6acace2
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,5 @@ If you want to create a custom widget that will autofill with the sms code, you
- `cancel()` to dispose the subscription of the SMS code from the native plugin, need to be called on your `dispose`.
- `codeUpdated()` called when the code is received, you can access the value with the field `code`.

### App Signature
To get the app signature at runtime just call the getter `getAppSignature` on `SmsAutoFill`. You can also find the sample code in example app.
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.jaumard.smsautofill;

import android.content.Context;
import android.content.ContextWrapper;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.util.Base64;
import android.util.Log;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;

/**
* This is a helper class to generate your message hash to be included in your SMS message.
*
* Without the correct hash, your app won't recieve the message callback. This only needs to be
* generated once per app and stored. Then you can remove this helper class from your code.
*/
public class AppSignatureHelper extends ContextWrapper {
public static final String TAG = AppSignatureHelper.class.getSimpleName();

private static final String HASH_TYPE = "SHA-256";
public static final int NUM_HASHED_BYTES = 9;
public static final int NUM_BASE64_CHAR = 11;

public AppSignatureHelper(Context context) {
super(context);
}

/**
* Get first app signature.
*/
public String getAppSignature(){
ArrayList<String> appSignatures = this.getAppSignatures();
if (!appSignatures.isEmpty()) {
return appSignatures.get(0);
}else{
return "NA";
}
}

/**
* Get all the app signatures for the current package
* @return
*/
public ArrayList<String> getAppSignatures() {
ArrayList<String> appCodes = new ArrayList<>();

try {
// Get all package signatures for the current package
String packageName = getPackageName();
PackageManager packageManager = getPackageManager();
Signature[] signatures = packageManager.getPackageInfo(packageName,
PackageManager.GET_SIGNATURES).signatures;

// For each signature create a compatible hash
for (Signature signature : signatures) {
String hash = hash(packageName, signature.toCharsString());
if (hash != null) {
appCodes.add(String.format("%s", hash));
}
}
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "Unable to find package to obtain hash.", e);
}
return appCodes;
}

private static String hash(String packageName, String signature) {
String appInfo = packageName + " " + signature;
try {
MessageDigest messageDigest = MessageDigest.getInstance(HASH_TYPE);
messageDigest.update(appInfo.getBytes(StandardCharsets.UTF_8));
byte[] hashSignature = messageDigest.digest();

// truncated into NUM_HASHED_BYTES
hashSignature = Arrays.copyOfRange(hashSignature, 0, NUM_HASHED_BYTES);
// encode into Base64
String base64Hash = Base64.encodeToString(hashSignature, Base64.NO_PADDING | Base64.NO_WRAP);
base64Hash = base64Hash.substring(0, NUM_BASE64_CHAR);

Log.d(TAG, String.format("pkg: %s -- hash: %s", packageName, base64Hash));
return base64Hash;
} catch (NoSuchAlgorithmException e) {
Log.e(TAG, "hash:NoSuchAlgorithm", e);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.Registrar;

import com.jaumard.smsautofill.AppSignatureHelper;
/**
* SmsAutoFillPlugin
*/
Expand All @@ -45,10 +46,13 @@ public class SmsAutoFillPlugin implements MethodCallHandler {
private Result pendingHintResult;
private MethodChannel channel;
private SmsBroadcastReceiver broadcastReceiver;
private final PluginRegistry.Registrar registrar;


private SmsAutoFillPlugin(MethodChannel channel, Registrar registrar) {
this.activity = registrar.activity();
this.channel = channel;
this.registrar = registrar;
registrar.addActivityResultListener(new PluginRegistry.ActivityResultListener() {

@Override
Expand Down Expand Up @@ -111,6 +115,11 @@ public void onFailure(@NonNull Exception e) {
pendingHintResult = result;
requestHint();
break;
case "getAppSignature":
AppSignatureHelper signatureHelper = new AppSignatureHelper(registrar.context());
String appSignature = signatureHelper.getAppSignature();
result.success(appSignature);
break;
default:
result.notImplemented();
break;
Expand Down
22 changes: 22 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class MyApp extends StatefulWidget {

class _MyAppState extends State<MyApp> {
String _code;
String signature = "{{ app signature }}";

@override
void initState() {
Expand Down Expand Up @@ -58,6 +59,27 @@ class _MyAppState extends State<MyApp> {
});
},
),
SizedBox(
height: 8.0
),
Divider(
height: 1.0
),
SizedBox(
height: 4.0
),
Text("App Signature : ${signature}"),
SizedBox(
height: 4.0
),
RaisedButton(
child: Text('Get app signature'),
onPressed: () async {
signature = await SmsAutoFill().getAppSignature;
setState((){});
},
),

],
),
),
Expand Down
20 changes: 10 additions & 10 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Generated by pub
# See https://www.dartlang.org/tools/pub/glossary#lockfile
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
async:
dependency: transitive
description:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.8"
version: "2.2.0"
boolean_selector:
dependency: transitive
description:
Expand Down Expand Up @@ -52,7 +52,7 @@ packages:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.3+1"
version: "0.12.5"
meta:
dependency: transitive
description:
Expand All @@ -73,7 +73,7 @@ packages:
name: pedantic
url: "https://pub.dartlang.org"
source: hosted
version: "1.4.0"
version: "1.7.0"
pin_input_text_field:
dependency: transitive
description:
Expand All @@ -87,7 +87,7 @@ packages:
name: quiver
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.1"
version: "2.0.3"
sky_engine:
dependency: transitive
description: flutter
Expand All @@ -99,14 +99,14 @@ packages:
path: ".."
relative: true
source: path
version: "0.0.1"
version: "1.0.0"
source_span:
dependency: transitive
description:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.4"
version: "1.5.5"
stack_trace:
dependency: transitive
description:
Expand All @@ -120,7 +120,7 @@ packages:
name: stream_channel
url: "https://pub.dartlang.org"
source: hosted
version: "1.6.8"
version: "2.0.0"
string_scanner:
dependency: transitive
description:
Expand All @@ -141,7 +141,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.2"
version: "0.2.5"
typed_data:
dependency: transitive
description:
Expand All @@ -157,4 +157,4 @@ packages:
source: hosted
version: "2.0.8"
sdks:
dart: ">=2.1.0 <3.0.0"
dart: ">=2.2.2 <3.0.0"
5 changes: 5 additions & 0 deletions lib/sms_autofill.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class SmsAutoFill {
Future<void> get listenForCode async {
await _channel.invokeMethod('listenForCode');
}

Future<String> get getAppSignature async {
final String appSignature = await _channel.invokeMethod('getAppSignature');
return appSignature;
}
}

class PinFieldAutoFill extends StatefulWidget {
Expand Down
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Generated by pub
# See https://www.dartlang.org/tools/pub/glossary#lockfile
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
collection:
dependency: transitive
Expand Down Expand Up @@ -47,4 +47,4 @@ packages:
source: hosted
version: "2.0.8"
sdks:
dart: ">=2.0.0 <3.0.0"
dart: ">=2.2.2 <3.0.0"

0 comments on commit 6acace2

Please sign in to comment.