Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use public key from a PEM file ? #25

Closed
christopheblin opened this issue Jul 26, 2021 · 1 comment
Closed

How to use public key from a PEM file ? #25

christopheblin opened this issue Jul 26, 2021 · 1 comment

Comments

@christopheblin
Copy link

I have the following python program

import base64
import ecdsa
import hashlib

digest = hashlib.sha256(b"Hello world").digest()
with open("key.pem") as f:
   public_key = ecdsa.VerifyingKey.from_pem(f.read())
signature = base64.b64decode("bxw1nS8Q39mnQVkAhvJctXuLHz4n0wUjLbE+phj1XlUeWDxl7DCK5bG4d7YrL7zGtAnUq3YT9AdrrAXjpwhCzQ==")

public_key.verify_digest(signature, digest)

pem file is :

-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEOC76WxZ/TMzJnRqv/cy9llDqSIMW
lplgREY3jMxDCksPGDDyepeg7fFdCu56N9Br9n5oWmj/iKULrglQehGiiA==
-----END PUBLIC KEY-----

I'd like to compare with your library, but I do not find how to create the Point/Signature from the PEM :

    const digest = crypto.createHash('sha256').update(Buffer.from('Hello world')).digest();

    const publicKey = secp.Point.fromPem(fs.readFileSync('key.pem')); //How to do here ?

    const signature = Buffer.from(
      'bxw1nS8Q39mnQVkAhvJctXuLHz4n0wUjLbE+phj1XlUeWDxl7DCK5bG4d7YrL7zGtAnUq3YT9AdrrAXjpwhCzQ==',
      'base64',
    );
    
    const isSigned = secp.verify(signature, digest, publicKey);
    assert(isSigned)

I also have x/y in JWK (so base64) in case it is easier than PEM :

  {
    "crv": "P-256",
    "kty": "EC",
    "x": "OC76WxZ/TMzJnRqv/cy9llDqSIMWlplgREY3jMxDCks=",
    "y": "Dxgw8nqXoO3xXQruejfQa/Z+aFpo/4ilC64JUHoRoog="
  }
@paulmillr
Copy link
Owner

Use a separate module to convert binary to PEM. PEM is out of scope for noble.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants