Skip to content

Commit

Permalink
Merge pull request #55 from TimothyClaeys/develop
Browse files Browse the repository at this point in the history
Develop: merge in bug fixes
  • Loading branch information
TimothyClaeys authored Apr 13, 2021
2 parents dfe8a77 + 0a09dbf commit 3bbabea
Show file tree
Hide file tree
Showing 15 changed files with 453 additions and 45 deletions.
59 changes: 58 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# pycose:snake: --- CBOR Object Signing and Encryption
[![Python package](https://github.com/TimothyClaeys/pycose/actions/workflows/python-package.yml/badge.svg)](https://github.com/TimothyClaeys/pycose/actions/workflows/python-package.yml)
[![Documentation Status](https://readthedocs.org/projects/pycose/badge/?version=latest)](https://pycose.readthedocs.io/en/latest/?badge=latest)

This project is a Python implementation of the IETF CBOR Encoded Message Syntax (COSE). COSE has reached RFC status and is now available at RFC 8152.

Expand Down Expand Up @@ -34,7 +35,63 @@ Additionally, based on the message type, other message fields can be added:
- _COSE recipients_ or _COSE signatures_ (for **MAC**, **Encrypt**, and **Sign** messages)

## Examples
A set of examples can be found [here](https://pycose.readthedocs.io/en/latest/examples.html)

### Encoding

```python
from binascii import unhexlify
from cose.messages import Enc0Message
from cose.keys import SymmetricKey

# Create a COSE Encrypt0 Message
msg = Enc0Message(
phdr={'ALG': 'A128GCM', 'IV': unhexlify(b'01010101010101010101010101010101')},
uhdr={'KID': b'[email protected]'},
payload='a secret message'.encode('utf-8')
)

# Create a COSE Symmetric Key
cose_key = SymmetricKey(key=unhexlify(b'000102030405060708090a0b0c0d0e0f'))
msg.key = cose_key

# Performs encryption and CBOR serialization
msg.encode()
b'\xd0\x83U\xa2\x01\x01\x05P\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\xa1\x04[email protected] \xc4\xaf\x85\xacJQ4\x93\x19\x93\xec\n\x18c\xa6\xe8\xc6n\xf4\xc9\xac\x161^\xe6\xfe\xcd\x9b.\x1cy\xa1'
```

### Decoding
```python
from binascii import unhexlify
from cose.messages import CoseMessage
from cose.keys import SymmetricKey

# message bytes (CBOR encoded)
msg = b'\xd0\x83U\xa2\x01\x01\x05P\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\xa1\x04[email protected] \xc4\xaf\x85\xacJQ4\x93\x19\x93\xec\n\x18c\xa6\xe8\xc6n\xf4\xc9\xac\x161^\xe6\xfe\xcd\x9b.\x1cy\xa1'

cose_msg = CoseMessage.decode(msg)

# Create a COSE Symmetric Key
cose_key = SymmetricKey(key=unhexlify(b'000102030405060708090a0b0c0d0e0f'))
cose_msg.key = cose_key

cose_msg.decrypt()
b'a secret message'
```

### More examples
More examples can be found [here](https://pycose.readthedocs.io/en/latest/examples.html)

## Testing

To run the test suite you need `pytest`:
```shell
$ pip install pytest
```
Move to the root of the repository and type:

```shell
$ pytest
```

## Cryptography

Expand Down
Loading

0 comments on commit 3bbabea

Please sign in to comment.