jerson / react-native-fast-openpgp

OpenPGP for react native made with golang for fast performance
https://www.npmjs.com/package/react-native-fast-openpgp
MIT License
45 stars 18 forks source link

convertPrivateKeyToPublicKey() return only part of PublicKey #70

Closed pondpiu closed 2 years ago

pondpiu commented 2 years ago

Usage

const { publicKey,privateKey } = generateKeyPair()
const result = await OpenPGP.convertPrivateKeyToPublicKey(privateKey);

the result seems to contain only part of the public key image

pondpiu commented 2 years ago

I've look into openpgp-mobile and add following test case in convert_test.go

func TestFastOpenPGP_ConvertPrivateKeyToPublicKey(t *testing.T) {
    options := &Options{
        Email:      "sample@sample.com",
        Name:       "Test",
        Comment:    "sample",
        Passphrase: "test",
        KeyOptions: &KeyOptions{
            CompressionLevel: 9,
            RSABits:          2048,
            Cipher:           "aes256",
            Compression:      "none",
            Hash:             "sha512",
        },
    }
    openPGP := NewFastOpenPGP()

    // Generate
    keyPair, err := openPGP.Generate(options)
    if err != nil {
        t.Fatal(err)
    }

    output, err := openPGP.ConvertPrivateKeyToPublicKey(keyPair.PrivateKey)
    if err != nil {
        t.Fatal(err)
    }

    t.Log("output:", output)
    t.Log("expected:", keyPair.PublicKey)

    if string(output) != string(keyPair.PublicKey) {
        t.Fatal("not same public key")
    }

}

running the test show that the output does not match the original public key

image

pondpiu commented 2 years ago

Logging each publicKey struct shows difference in PublicKey, n, and e

Original publicKey struct : &{Version:4 CreationTime:2022-09-01 03:47:30 +0000 UTC PubKeyAlgo:1 PublicKey:0xc00011e760 Fingerprint:[84 188 233 74 132 47 136 38 24 41 171 151 247 132 254 126 185 255 163 90] KeyId:17835660244533879642 IsSubkey:false n:0xc000156640 e:0xc000156680 p:<nil> q:<nil> g:<nil> y:<nil> oid:<nil> kdf:<nil>}
Parsed publicKey struct : &{Version:4 CreationTime:2022-09-01 03:47:30 +0000 UTC PubKeyAlgo:1 PublicKey:0xc00011e800 Fingerprint:[84 188 233 74 132 47 136 38 24 41 171 151 247 132 254 126 185 255 163 90] KeyId:17835660244533879642 IsSubkey:false n:0xc000156980 e:0xc0001569c0 p:<nil> q:<nil> g:<nil> y:<nil> oid:<nil> kdf:<nil>}
jerson commented 2 years ago

Hi @pondpiu thanks for report the issue, im gonna take a look later today and add that assertion, I will let now now

jerson commented 2 years ago

hi @pondpiu good news, after a fix in openpgp-mobile seems working fine, gonna run some test and prepare a release

=== RUN   TestFastOpenPGP_ConvertPrivateKeyToPublicKeyAndGenerate
    convert_test.go:115: output: -----BEGIN PGP PUBLIC KEY BLOCK-----
        Version: openpgp-mobile

        xsBNBGMQyCsBCADMkCMIffrm/IiOA78vaPdWCkxttQE+9jOEUDlrYmU4mkAG3OXN
        g/4eI89+ZCIHl018E4FNro3LNNbQx3f0KK6oyEpKqUVpTgip4UMTWYvsQ4g+DZU+
        RJqY7oxxOqjtT/aWILXiD6E89g8PO0rksa6i0NX0rLLMGKY6Vuo4nmQYvfi7QBmm
        Hj15jIHid//Ky8YN6b9m16kNaoWuF0dZU/gHR5i6UNj+oUzrNBe1GzqRf7QN8m7q
        5ApvS/y4gixmRYgAWFOSlcZg3BLmDs4otXJu4DBYTaolcPlH2wGQ3QXL51iXHwUa
        GZlIz/vEcIFyk9M64Uf6a+yjEmXeJf4QVT+jABEBAAHNIVRlc3QgKHNhbXBsZSkg
        PHNhbXBsZUBzYW1wbGUuY29tPsLAigQTAQoAPgUCYxDIKwmQ2UoxmLrirVEWIQS7
        bqwekKBwv31ac0vZSjGYuuKtUQIbAwIeAQIZAQMLCQcDFQoIAhYAAiIBAAAM1Qf/
        eTHNCZWjFOh8LWXHrpxb+RZFBDhY281PFNUxtZDX3YgeppIlKu93lxas5a6rrGGq
        fzQgUJFono9oz9dE1hy2e1UwCwcP8TM7ADmINEGy3Tu0/CDCKudBaLnzk4mXxVtI
        IkVRpzCFbU4DpG3Gn5ipl14PxX+SrtNne6S5X+1NvxAy0kcnW6itgD9uFti+pGmq
        Sb/stDyMgs3iPtJUrUkYLFed6azKAxKWRTiyQGNvDWXmqI2r5kpYY7NRmc8ONcon
        BUH9JFUdzufy9Gsd2x2xsinXJv5Zp+UyYYvGoZXlru9EqQTSfm5nB89csz65Z2va
        APCHCE6qaVWxngrZFR8QBs7ATQRjEMgrAQgA0uQ7ly/9hQSHCStHKM7nDHag5utc
        +UOFFXIS9J9Z91+HV+om58rEbl6s8KutRMQwOQYdcnQDV2lewmw/+LkdbdaJU9cJ
        vATb0yryjNpF30RHAvbkCRysakHSbCZXNfn7tyz3RKAKBb7EzqBQEjy2xW6m/VWm
        XgBiyayZmMLjlI5SRMHoxWq8ZdyskMt4ya5NKjPrSJS7xZJfUrWhWYEBjQovIEKp
        lu6HIJXjFQMGNyij75cKBxJ9azj6rvEim7XP5v8Af2xvheRL9s2VmxjRH1nu6CKw
        Dr3NFfn4fgyswXmk7LaYzCxDdzRA0NCPyHtZpfNutX9ESGce5nGXA41ooQARAQAB
        wsB2BBgBCgAqBQJjEMgrCZDZSjGYuuKtURYhBLturB6QoHC/fVpzS9lKMZi64q1R
        AhsMAADdiAf+J53WHLVWdtaINPuid2ujFPkxhssrnquGNydzmSM/D5EaUcOooJUM
        ExubXBI5FteqOvlgencD/BF3ICVRfGKSiIZfRat8GISf2tKJI3jklz00PPsrOUO8
        4oxjIQsYJWD4To0V37cUZhGyJq5Nl1riQGY1Z1TH9BqcZc7hSSXQooeWvxrmFCEu
        sE7zpFzH3xwZxopFuvN4edVhcncawmpputWjkE4RJfXH53b9hXv9FMqIuGJ80JxG
        5X9RvyVJnMUnAnJ69wlGKw5bWCEj8BiIW5yA4osnz2KsgHgyMcrJQQuZO551W+/m
        hxOQzhg1XEmaooVBN2eCIL+DYVa7Qcp9aA==
        =3+tS
        -----END PGP PUBLIC KEY BLOCK-----
    convert_test.go:116: expected: -----BEGIN PGP PUBLIC KEY BLOCK-----
        Version: openpgp-mobile

        xsBNBGMQyCsBCADMkCMIffrm/IiOA78vaPdWCkxttQE+9jOEUDlrYmU4mkAG3OXN
        g/4eI89+ZCIHl018E4FNro3LNNbQx3f0KK6oyEpKqUVpTgip4UMTWYvsQ4g+DZU+
        RJqY7oxxOqjtT/aWILXiD6E89g8PO0rksa6i0NX0rLLMGKY6Vuo4nmQYvfi7QBmm
        Hj15jIHid//Ky8YN6b9m16kNaoWuF0dZU/gHR5i6UNj+oUzrNBe1GzqRf7QN8m7q
        5ApvS/y4gixmRYgAWFOSlcZg3BLmDs4otXJu4DBYTaolcPlH2wGQ3QXL51iXHwUa
        GZlIz/vEcIFyk9M64Uf6a+yjEmXeJf4QVT+jABEBAAHNIVRlc3QgKHNhbXBsZSkg
        PHNhbXBsZUBzYW1wbGUuY29tPsLAigQTAQoAPgUCYxDIKwmQ2UoxmLrirVEWIQS7
        bqwekKBwv31ac0vZSjGYuuKtUQIbAwIeAQIZAQMLCQcDFQoIAhYAAiIBAAAM1Qf/
        eTHNCZWjFOh8LWXHrpxb+RZFBDhY281PFNUxtZDX3YgeppIlKu93lxas5a6rrGGq
        fzQgUJFono9oz9dE1hy2e1UwCwcP8TM7ADmINEGy3Tu0/CDCKudBaLnzk4mXxVtI
        IkVRpzCFbU4DpG3Gn5ipl14PxX+SrtNne6S5X+1NvxAy0kcnW6itgD9uFti+pGmq
        Sb/stDyMgs3iPtJUrUkYLFed6azKAxKWRTiyQGNvDWXmqI2r5kpYY7NRmc8ONcon
        BUH9JFUdzufy9Gsd2x2xsinXJv5Zp+UyYYvGoZXlru9EqQTSfm5nB89csz65Z2va
        APCHCE6qaVWxngrZFR8QBs7ATQRjEMgrAQgA0uQ7ly/9hQSHCStHKM7nDHag5utc
        +UOFFXIS9J9Z91+HV+om58rEbl6s8KutRMQwOQYdcnQDV2lewmw/+LkdbdaJU9cJ
        vATb0yryjNpF30RHAvbkCRysakHSbCZXNfn7tyz3RKAKBb7EzqBQEjy2xW6m/VWm
        XgBiyayZmMLjlI5SRMHoxWq8ZdyskMt4ya5NKjPrSJS7xZJfUrWhWYEBjQovIEKp
        lu6HIJXjFQMGNyij75cKBxJ9azj6rvEim7XP5v8Af2xvheRL9s2VmxjRH1nu6CKw
        Dr3NFfn4fgyswXmk7LaYzCxDdzRA0NCPyHtZpfNutX9ESGce5nGXA41ooQARAQAB
        wsB2BBgBCgAqBQJjEMgrCZDZSjGYuuKtURYhBLturB6QoHC/fVpzS9lKMZi64q1R
        AhsMAADdiAf+J53WHLVWdtaINPuid2ujFPkxhssrnquGNydzmSM/D5EaUcOooJUM
        ExubXBI5FteqOvlgencD/BF3ICVRfGKSiIZfRat8GISf2tKJI3jklz00PPsrOUO8
        4oxjIQsYJWD4To0V37cUZhGyJq5Nl1riQGY1Z1TH9BqcZc7hSSXQooeWvxrmFCEu
        sE7zpFzH3xwZxopFuvN4edVhcncawmpputWjkE4RJfXH53b9hXv9FMqIuGJ80JxG
        5X9RvyVJnMUnAnJ69wlGKw5bWCEj8BiIW5yA4osnz2KsgHgyMcrJQQuZO551W+/m
        hxOQzhg1XEmaooVBN2eCIL+DYVa7Qcp9aA==
        =3+tS
        -----END PGP PUBLIC KEY BLOCK-----
--- PASS: TestFastOpenPGP_ConvertPrivateKeyToPublicKeyAndGenerate (0.37s)
PASS
jerson commented 2 years ago

version released, could you try using v2.4.2 https://www.npmjs.com/package/react-native-fast-openpgp/v/2.4.2

pondpiu commented 2 years ago

@jerson Version 2.4.2 works perfectly. Thank you. 😀