Handle failed encryption and decryption attempts, return an error.

This commit is contained in:
Greg 2019-11-16 00:27:25 -05:00
parent f5a3da4398
commit 0dc003bd57
4 changed files with 37 additions and 9 deletions

View File

@ -63,6 +63,11 @@ public class PgpConnect extends Fragment {
Log.d("gio", "onActivityResult(" + requestCode + "): " + resultCode); Log.d("gio", "onActivityResult(" + requestCode + "): " + resultCode);
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
//activityResult(requestCode, resultCode); //activityResult(requestCode, resultCode);
if (resultCode != Activity.RESULT_OK) {
Log.d("gio", "onActivityResult: not OK");
stringResult(requestCode, null);
return;
}
switch (data.getAction()) { switch (data.getAction()) {
case OpenPgpApi.ACTION_DECRYPT_VERIFY: { case OpenPgpApi.ACTION_DECRYPT_VERIFY: {
Log.d("gio", "action decrypt"); Log.d("gio", "action decrypt");

View File

@ -66,8 +66,12 @@ Decrypt(JNIEnv* env, jobject p, char* cdata, int datalen, int chint) {
} }
void stringResult(JNIEnv* env, jclass cls, jint requestCode, jobject response) { void stringResult(JNIEnv* env, jclass cls, jint requestCode, jobject response) {
char* str = (*env)->GetStringUTFChars(env, response, NULL); if (response == 0) {
goStringResult(env, cls, requestCode, str); goStringResult(env, cls, requestCode, 0);
} else {
char* str = (*env)->GetStringUTFChars(env, response, NULL);
goStringResult(env, cls, requestCode, str);
}
} }
const char* const char*

View File

@ -70,7 +70,7 @@ var (
chint int chint int
) )
func (p PGP) Decrypt(data string) string { func (p PGP) Decrypt(data string) (string, error) {
ch := make(chan string) ch := make(chan string)
chmux.Lock() chmux.Lock()
if fragChans == nil { if fragChans == nil {
@ -87,10 +87,15 @@ func (p PGP) Decrypt(data string) string {
retc := C.Decrypt(env, (C.jobject)(p), (*C.char)(unsafe.Pointer(cdata)), (C.int)(len(data)), C.int(curint)) retc := C.Decrypt(env, (C.jobject)(p), (*C.char)(unsafe.Pointer(cdata)), (C.int)(len(data)), C.int(curint))
defer C.free(unsafe.Pointer(retc)) defer C.free(unsafe.Pointer(retc))
}) })
return <-ch switch x := <-ch; {
case x == "":
return "", fmt.Errorf("Decryption failed")
default:
return x, nil
}
} }
func (p PGP) Encrypt(id, data string) string { func (p PGP) Encrypt(id, data string) (string, error) {
ch := make(chan string) ch := make(chan string)
chmux.Lock() chmux.Lock()
if fragChans == nil { if fragChans == nil {
@ -109,7 +114,12 @@ func (p PGP) Encrypt(id, data string) string {
retc := C.Encrypt(env, (C.jobject)(p), (*C.char)(unsafe.Pointer(cid)), (C.int)(len(id)), (*C.char)(unsafe.Pointer(cdata)), (C.int)(len(data)), C.int(curint)) retc := C.Encrypt(env, (C.jobject)(p), (*C.char)(unsafe.Pointer(cid)), (C.int)(len(id)), (*C.char)(unsafe.Pointer(cdata)), (C.int)(len(data)), C.int(curint))
defer C.free(unsafe.Pointer(retc)) defer C.free(unsafe.Pointer(retc))
}) })
return <-ch switch x := <-ch; {
case x == "":
return "", fmt.Errorf("Encryption failed")
default:
return x, nil
}
} }
func RunInJVM(f func(env *C.JNIEnv)) { func RunInJVM(f func(env *C.JNIEnv)) {

View File

@ -47,10 +47,19 @@ func callJni() {
labchan <- fmt.Sprintf("Activity = %d", h.Activity) labchan <- fmt.Sprintf("Activity = %d", h.Activity)
time.Sleep(time.Second / 5) time.Sleep(time.Second / 5)
val := pgp.Encrypt("greg_pomerantz@yahoo.com", "hi there") val, err := pgp.Encrypt("greg_pomerantz@yahoo.com", "hi there")
//val := pgp.Encrypt("root@example.com", "hi there") if err != nil {
fmt.Printf("Encryption error: %s", err)
labchan <- err.Error()
return
}
log.Print("val = ", val) log.Print("val = ", val)
val2 := pgp.Decrypt(val) val2, err := pgp.Decrypt(val)
if err != nil {
fmt.Printf("Decryption error: %s", err)
labchan <- err.Error()
return
}
labchan <- "result = " + val2 labchan <- "result = " + val2
} }