Skip to content

Instantly share code, notes, and snippets.

@jpopesculian
Last active July 30, 2021 12:33
Show Gist options
  • Save jpopesculian/a1056f80c6fbb2df2ac42fe5732845e8 to your computer and use it in GitHub Desktop.
Save jpopesculian/a1056f80c6fbb2df2ac42fe5732845e8 to your computer and use it in GitHub Desktop.
CBOR to base64 public key

Setup

To setup, first create a virtual env

python3 -m virtualenv venv

activate the virtual env

source venv/bin/activate

install the requirements

pip3 install -r requirements.txt

Run

activate the virutal env if not activated

source venv/bin/activate

and run the script

python3 convert.py BASE64_WEBSAFE_ENCODED_CBOR_PUBLIC_KEY
import sys
import base64
from fido2 import cbor
def cbor_to_base64_pbk(cbor_pbk):
padding = "="
cbor_pbk = cbor_pbk + padding * (len(cbor_pbk) % 4)
pbk = base64.b64decode(cbor_pbk.replace("_", "/").replace("-", "+").encode("utf-8"))
data = cbor.decode(pbk)
return base64.urlsafe_b64encode(bytes([4]) + data[-2] + data[-3]).decode('utf-8').rstrip("=")
if __name__ == "__main__":
if len(sys.argv) < 2:
exit("please provide a public key to convert from CBOR")
cbor_pbk = sys.argv[1]
print(cbor_to_base64_pbk(cbor_pbk))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment