Skip to content

Commit

Permalink
Merge pull request #14 from regulaforensics/release-6.1
Browse files Browse the repository at this point in the history
Release 6.1
  • Loading branch information
DzmitrySmaliakou authored Feb 23, 2024
2 parents 740bb09 + 563c7f5 commit d27cd1a
Show file tree
Hide file tree
Showing 118 changed files with 2,297 additions and 408 deletions.
15 changes: 10 additions & 5 deletions CertificatePinningSample/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ plugins {
}

android {
compileSdkVersion 32
compileSdk 32

defaultConfig {
applicationId "com.regula.facesamplekotlin"
minSdkVersion 24
targetSdkVersion 32
minSdk 24
targetSdk 32
versionCode 1
versionName "1.0"
}
Expand All @@ -24,6 +24,11 @@ android {
buildFeatures {
viewBinding true
}

//Required to add
aaptOptions {
noCompress "Regula/faceSdkResource.dat"
}
}

dependencies {
Expand All @@ -34,9 +39,9 @@ dependencies {
implementation "androidx.core:core-ktx:1.7.0"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

implementation ('com.regula.face:api:5.2.+@aar'){
implementation ('com.regula.face:api:6.1.+@aar'){
transitive = true
}

implementation ('com.regula.face:core:5.2.+@aar')
implementation ('com.regula.face.core:basic:6.1.+@aar')
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
package com.regula.facesamplekotlin

import android.net.http.X509TrustManagerExtensions
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Base64
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.regula.common.http.HttpRequestBuilder
import com.regula.facesamplekotlin.databinding.ActivityMainBinding
import com.regula.facesdk.FaceSDK
import com.regula.facesdk.exception.InitException
import com.regula.facesdk.listener.NetworkInterceptorListener
import java.security.KeyStore
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException
import java.security.cert.Certificate
import java.security.cert.CertificateException
import java.security.cert.X509Certificate
import java.util.Arrays
import javax.net.ssl.HttpsURLConnection
import javax.net.ssl.SSLException
import javax.net.ssl.SSLPeerUnverifiedException
import javax.net.ssl.TrustManagerFactory
import javax.net.ssl.X509TrustManager

class MainActivity : AppCompatActivity() {

class MainActivity : AppCompatActivity(), NetworkInterceptorListener {

val VALID_PINS = setOf("/5RKFaPkCjAzvsEZHOlYqncYADaLIG5VfTmhsBbkaBk=")

@Transient
private lateinit var binding: ActivityMainBinding
Expand All @@ -21,15 +40,17 @@ class MainActivity : AppCompatActivity() {
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

FaceSDK.Instance().init(this) { status: Boolean, e: InitException? ->
FaceSDK.Instance().initialize(this) { status: Boolean, e: InitException? ->
if (!status) {
Toast.makeText(
this@MainActivity,
"Init finished with error: " + if (e != null) e.message else "",
Toast.LENGTH_LONG
).show()
return@init
return@initialize
}
FaceSDK.Instance().setLogs(true)
FaceSDK.Instance().setNetworkInterceptorListener(this@MainActivity)
(Handler(Looper.getMainLooper())).post {
binding.startBtn.isEnabled = true
}
Expand All @@ -45,4 +66,84 @@ class MainActivity : AppCompatActivity() {
}
}
}

override fun onPrepareRequest(connection: HttpRequestBuilder?) {
val trustManagerFactory = TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm()
)
trustManagerFactory.init(null as KeyStore?)
// Find first X509TrustManager in the TrustManagerFactory
// Find first X509TrustManager in the TrustManagerFactory
var x509TrustManager: X509TrustManager? = null
for (trustManager in trustManagerFactory.trustManagers) {
if (trustManager is X509TrustManager) {
x509TrustManager = trustManager
break
}
}
val trustManagerExt = X509TrustManagerExtensions(x509TrustManager)
connection?.connection?.let {
println("validatePinning")
try {
connection.connection.connect()
} catch (e: Exception) {
println("Error connect: " + e)
}
validatePinning(trustManagerExt, connection.connection as HttpsURLConnection, VALID_PINS)
}

}

@Throws(SSLException::class)
private fun validatePinning(
trustManagerExt: X509TrustManagerExtensions,
conn: HttpsURLConnection, validPins: Set<String>
) {
var certChainMsg = ""
try {
val md = MessageDigest.getInstance("SHA-256")
val trustedChain: List<X509Certificate> = trustedChain(trustManagerExt, conn)
println("trustedChain count: " + trustedChain.size)
for (cert: X509Certificate in trustedChain) {
val publicKey: ByteArray = cert.publicKey.encoded
md.update(publicKey, 0, publicKey.size)
val pin: String = Base64.encodeToString(
md.digest(),
Base64.NO_WRAP
)
certChainMsg += (" sha256/" + pin + " : " +
cert.subjectDN.toString()) + "\n"
if (validPins.contains(pin)) {
return
}
}
} catch (e: NoSuchAlgorithmException) {
throw SSLException(e)
}
throw SSLPeerUnverifiedException(
"Certificate pinning " +
"failure\n Peer certificate chain:\n" + certChainMsg
)
}

@Throws(SSLException::class)
private fun trustedChain(
trustManagerExt: X509TrustManagerExtensions,
conn: HttpsURLConnection
): List<X509Certificate> {
val serverCerts: Array<Certificate> = conn.serverCertificates
val untrustedCerts: Array<X509Certificate> = Arrays.copyOf(
serverCerts,
serverCerts.size, Array<X509Certificate>::class.java
)
val host = conn.url.host
try {
return trustManagerExt.checkServerTrusted(
untrustedCerts,
"RSA", host
)
} catch (e: CertificateException) {
throw SSLException(e)
}
}
}
15 changes: 10 additions & 5 deletions FaceSample/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ plugins {
}

android {
compileSdkVersion 32
compileSdk 32

defaultConfig {
applicationId "com.regula.facesample"
minSdkVersion 21
targetSdkVersion 32
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"

Expand All @@ -21,6 +21,11 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

//Required to add
aaptOptions {
noCompress "Regula/faceSdkResource.dat"
}
}

dependencies {
Expand All @@ -34,9 +39,9 @@ dependencies {

implementation "androidx.recyclerview:recyclerview:1.2.1"

implementation ('com.regula.face:api:5.2.+@aar'){
implementation ('com.regula.face:api:6.1.+@aar'){
transitive = true
}

implementation ('com.regula.face:core:5.2.+@aar')
implementation ('com.regula.face.core:basic:6.1.+@aar')
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.regula.facesample.adapter.CategoryAdapter;
import com.regula.facesample.data.CategoryDataProvider;
import com.regula.facesdk.FaceSDK;
import com.regula.facesdk.callback.InitCallback;
import com.regula.facesdk.exception.InitException;

public class MainActivity extends Activity {

Expand All @@ -31,7 +28,7 @@ public void onCreate(Bundle savedInstanceState) {

recyclerView.setAdapter(new CategoryAdapter(this, dataProvider));

FaceSDK.Instance().init(this, (status, e) -> {
FaceSDK.Instance().initialize(this, (status, e) -> {
if (!status) {
Toast.makeText(MainActivity.this, "Init finished with error: " + (e != null ? e.getMessage() : ""), Toast.LENGTH_LONG).show();
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.Button;
Expand Down Expand Up @@ -236,12 +235,17 @@ private void matchFaces(Bitmap first, Bitmap second) {
FaceSDK.Instance().matchFaces(matchRequest, matchFacesResponse -> {
MatchFacesSimilarityThresholdSplit split =
new MatchFacesSimilarityThresholdSplit(matchFacesResponse.getResults(), 0.75d);
Double similarity = null;
if (split.getMatchedFaces().size() > 0) {
double similarity = split.getMatchedFaces().get(0).getSimilarity();
similarity = split.getMatchedFaces().get(0).getSimilarity();
} else if (split.getUnmatchedFaces().size() > 0) {
similarity = split.getUnmatchedFaces().get(0).getSimilarity();
}

if (similarity != null)
textViewSimilarity.setText("Similarity: " + String.format("%.2f", similarity * 100) + "%");
} else {
else
textViewSimilarity.setText("Similarity: null");
}

buttonMatch.setEnabled(true);
buttonLiveness.setEnabled(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.regula.facesample.items.basic;

import android.content.Context;
import android.widget.Toast;

import com.regula.facesample.items.CategoryItem;
import com.regula.facesample.util.FaceCaptureResponseUtil;
import com.regula.facesdk.FaceSDK;

/**
Expand All @@ -14,7 +16,7 @@ public class FaceCaptureDefaultItem extends CategoryItem {

@Override
public void onItemSelected(Context context) {
FaceSDK.Instance().presentFaceCaptureActivity(context, faceCaptureResponse -> { });
FaceSDK.Instance().presentFaceCaptureActivity(context, faceCaptureResponse -> FaceCaptureResponseUtil.response(context, faceCaptureResponse));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.regula.facesample.items.basic;

import android.content.Context;
import android.widget.Toast;

import com.regula.facesample.items.CategoryItem;
import com.regula.facesample.util.LivenessResponseUtil;
import com.regula.facesdk.FaceSDK;

/**
Expand All @@ -14,7 +16,7 @@ public class LivenessDefaultItem extends CategoryItem {

@Override
public void onItemSelected(Context context) {
FaceSDK.Instance().startLiveness(context, livenessResponse -> { });
FaceSDK.Instance().startLiveness(context, livenessResponse -> LivenessResponseUtil.response(context, livenessResponse));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.regula.facesample.items.CategoryItem;
import com.regula.facesample.items.customization.fragment.AdvancedCustomUiFragment;
import com.regula.facesample.util.LivenessResponseUtil;
import com.regula.facesdk.FaceSDK;
import com.regula.facesdk.configuration.LivenessConfiguration;

Expand All @@ -19,7 +20,7 @@ public void onItemSelected(Context context) {
LivenessConfiguration configuration = new LivenessConfiguration.Builder()
.registerUiFragmentClass(AdvancedCustomUiFragment.class)
.build();
FaceSDK.Instance().startLiveness(context, configuration, livenessResponse -> {});
FaceSDK.Instance().startLiveness(context, configuration, livenessResponse -> LivenessResponseUtil.response(context, livenessResponse));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.regula.facesample.items.CategoryItem;
import com.regula.facesample.items.customization.fragment.BasicCustomUiFragment;
import com.regula.facesample.util.LivenessResponseUtil;
import com.regula.facesdk.FaceSDK;
import com.regula.facesdk.configuration.LivenessConfiguration;

Expand All @@ -19,7 +20,7 @@ public void onItemSelected(Context context) {
LivenessConfiguration configuration = new LivenessConfiguration.Builder()
.registerUiFragmentClass(BasicCustomUiFragment.class)
.build();
FaceSDK.Instance().startLiveness(context, configuration, livenessResponse -> {});
FaceSDK.Instance().startLiveness(context, configuration, livenessResponse -> LivenessResponseUtil.response(context, livenessResponse));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.regula.facesample.items.CategoryItem;
import com.regula.facesample.items.customization.fragment.ButtonsColorFragment;
import com.regula.facesample.util.FaceCaptureResponseUtil;
import com.regula.facesdk.FaceSDK;
import com.regula.facesdk.configuration.FaceCaptureConfiguration;
import com.regula.facesdk.configuration.LivenessConfiguration;
Expand All @@ -20,7 +21,7 @@ public void onItemSelected(Context context) {
.setCameraSwitchEnabled(true)
.registerUiFragmentClass(ButtonsColorFragment.class)
.build();
FaceSDK.Instance().presentFaceCaptureActivity(context, configuration, faceCaptureResponse -> { });
FaceSDK.Instance().presentFaceCaptureActivity(context, configuration, faceCaptureResponse -> FaceCaptureResponseUtil.response(context, faceCaptureResponse));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.regula.facesample.items.CategoryItem;
import com.regula.facesample.items.customization.fragment.FlashButtonFragment;
import com.regula.facesample.util.FaceCaptureResponseUtil;
import com.regula.facesdk.FaceSDK;
import com.regula.facesdk.configuration.FaceCaptureConfiguration;
import com.regula.facesdk.configuration.LivenessConfiguration;
Expand All @@ -21,7 +22,7 @@ public void onItemSelected(Context context) {
.registerUiFragmentClass(FlashButtonFragment.class)
.setCameraSwitchEnabled(true)
.build();
FaceSDK.Instance().presentFaceCaptureActivity(context, configuration, livenessResponse -> {});
FaceSDK.Instance().presentFaceCaptureActivity(context, configuration, faceCaptureResponse -> FaceCaptureResponseUtil.response(context, faceCaptureResponse));
}

@Override
Expand Down
Loading

0 comments on commit d27cd1a

Please sign in to comment.