lvh / caesium

Modern cryptography (libsodium/NaCl) for Clojure

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why does sign/keypair! produce negative numbers?

TomLisankie opened this issue · comments

I've begun the process of writing a Clojure implementation of the Scuttlebutt Protocol. I need to be able to generate an Ed25519 keypair for use as a user's identity. The public key (at the very least) should be UTF-8 encoded so that it is readable by people. However, when I generate a keypair to serve as a user identity, both the public and private keys usually have negative integers representing each byte. This is a problem since UTF-8 bytes are represented as non-negative integers. So when I convert the ByteBuffer produced by sign/keypair to a string using the byte-streams library, a whole bunch of the characters show up as the infamous "�" showing that the byte was not recognized as a valid UTF-8 char.

My question is is there any way to prevent sign/keypair from using negative numbers as bytes? There doesn't seem to be but I figured I should ask. Should I even be using sign/keypair for this?

commented

Bytes on the JVM are signed: negative numbers are not a bug. But more importantly there's no guarantee that an Ed25519 key pair is a valid UTF-8 sequence, nor that it can be interpreted as a sequence of code points encodeable as UTF-8 (whatever that means in practice).

If you want to represent an arbitrary sequence of bytes as human readable text you're going to need an encoding scheme like hex encoding, base32, base64... or something like a QR code.

Yeah turns out I misunderstood what I have to do for implementation of the protocol 🤦. I need to encode it as base64, not UTF-8.