{"id":24093180,"url":"https://github.com/tramires/cose-w","last_synced_at":"2025-05-07T05:05:30.850Z","repository":{"id":176304214,"uuid":"655305024","full_name":"tramires/cose-w","owner":"tramires","description":"WebAssembly COSE RFC 8152 library to encode/decode COSE messages in JS.","archived":false,"fork":false,"pushed_at":"2024-09-05T09:51:21.000Z","size":158,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-07T05:05:24.701Z","etag":null,"topics":["cbor","cose","cryptography","encrypt","js","mac","npm","rust","signature","wasm"],"latest_commit_sha":null,"homepage":"https://datatracker.ietf.org/doc/rfc8152/","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tramires.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-06-18T14:00:52.000Z","updated_at":"2024-09-05T09:51:24.000Z","dependencies_parsed_at":null,"dependency_job_id":"7577c571-a587-490e-b45d-216e7ef7a223","html_url":"https://github.com/tramires/cose-w","commit_stats":null,"previous_names":["tramires/cose-w"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tramires%2Fcose-w","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tramires%2Fcose-w/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tramires%2Fcose-w/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tramires%2Fcose-w/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tramires","download_url":"https://codeload.github.com/tramires/cose-w/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252817010,"owners_count":21808705,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cbor","cose","cryptography","encrypt","js","mac","npm","rust","signature","wasm"],"created_at":"2025-01-10T09:25:34.510Z","updated_at":"2025-05-07T05:05:30.831Z","avatar_url":"https://github.com/tramires.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![npm version](https://badge.fury.io/js/cose-w.svg)](https://badge.fury.io/js/cose-w)\n\n# cose-w\n\n WebAssembly COSE [RFC 8152](https://tools.ietf.org/html/rfc8152) library to encode/decode COSE messages in JS.\n\n See full documentation [here](docs/).\n\n# COSE \n\nCOSE is a concise binary data format that protects the payload of the message with a set of cryptographic operations.\n\nThe COSE [RFC 8152](https://tools.ietf.org/html/rfc8152) specifies the following 6 types of COSE messages:\n\n- **cose-sign1**: A digitally signed COSE message with a single signer.\n- **cose-sign**: A digitally signed COSE message with a signers bucket.\n- **cose-encrypt0**: An encrypted COSE message with a single recipient.\n- **cose-encrypt**: An encrypted COSE message with a recipients bucket.\n- **cose-mac0**: A MAC tagged COSE message with a single recipient.\n- **cose-encrypt**: A MAC tagged COSE message with a recipients bucket.\n\n# Examples\n\nThe following examples, demonstrate how to encode and decode the basic COSE messages (cose-sign1, cose-encrypt0, cose-mac0), examples of other use cases and cose message types\ncan be found in the respective documentation.\n\n## cose-sign1\n\nEncode/decode cose-sign1.\n\n### Encode cose-sign1 \n```js\nvar {\n  CoseKey,\n  CoseHeader,\n  CoseMessage,\n  Alg,\n  Kty,\n  Crv,\n  KeyOp\n} = require('cose-w');\n\n// Message to sign, \"This is the content.\"\nlet msg = Buffer.from('546869732069732074686520636F6E74656E742E', 'hex');\n\n// Prepare cose-key\nlet key = new CoseKey();\nkey.kty = Kty.ec2;\nkey.alg = Alg.es256;\nkey.crv = Crv.p_256;\nkey.d = Buffer.from('57c92077664146e876760c9520d054aa93c3afb04e306705db6090308507b4d3', 'hex');\nkey.key_ops = [KeyOp.sign];\n\n// Prepare cose-sign1 header\nlet header = new CoseHeader();\nheader.set_alg(Alg.es256, true, false);\nheader.set_kid([49, 49], true, false);\n\n// Generate CoseSign and encode the cose-sign1 final message\nlet signer = CoseMessage.new_sign();\nsigner.header = header;\nsigner.payload = msg;\nsigner.key(key);\nsigner.secure_content(null);\nlet bytes = signer.encode(true);\n```\n\n### Decode cose-sign1 \n```js\nvar {\n  CoseKey,\n  CoseMessage,\n  Alg,\n  Kty,\n  Crv,\n  KeyOp\n} = require('cose-w');\n\n// Prepare cose-key\nlet key = new CoseKey();\nkey.kty = Kty.ec2;\nkey.alg = Alg.es256;\nkey.crv = Crv.p_256;\nkey.x = Buffer.from('bac5b11cad8f99f9c72b05cf4b9e26d244dc189f745228255a219a86d6a09eff', 'hex');\nkey.y = Buffer.from('20138bf82dc1b6d562be0fa54ab7804a3a64b6d72ccfed6b6fb6ed28bbfc117e', 'hex');\nkey.key_ops = [KeyOp.verify];\n\n// Prepare CoseSign with the cose-sign1 bytes\nlet verify = CoseMessage.new_sign();\nverify.bytes = Buffer.from(\"d28447a2012604423131a054546869732069732074686520636f6e74656e742e58405e84ce5812b0966e6919ff1ac15c030666bae902c0705d1e0a5fbac828437c63b0bb87a95a456835f4d115850adefcf0fd0a5c26027140c10d3e20a890c5eaa7\", \"hex\");\n\n// Init decoding and add key\nverify.init_decoder(null);\nverify.key(key);\n\n// Verify cose-sign1\nlet payload = verify.decode(null, null);\n```\n\n## cose-encrypt0\n\nEncode/decode cose-encrypt0.\n\n### Encode cose-encrypt0\n```js\nvar {\n  CoseKey,\n  CoseHeader,\n  CoseMessage,\n  Alg,\n  Kty,\n  KeyOp\n} = require('cose-w');\n\n// Message to encrypt, \"This is the content.\"\nlet msg = Buffer.from('546869732069732074686520636F6E74656E742E', 'hex');\n\n// Prepare cose-key\nlet key = new CoseKey();\nkey.kty = Kty.symmetric;\nkey.alg = Alg.chacha20;\nkey.k = Buffer.from('849b57219dae48de646d07dbb533566e976686457c1491be3a76dcea6c427188', 'hex');\nkey.key_ops = [KeyOp.encrypt];\n\n// Prepare cose-encrypt0 header\nlet header = new CoseHeader();\nheader.set_alg(Alg.chacha20, true, false);\nheader.set_iv(Buffer.from('89f52f65a1c580933b5261a7', 'hex'), true, false);\n\n// Generate CoseEncrypt and encode the cose-encrypt0 final message\nlet enc = CoseMessage.new_encrypt();\nenc.header = header;\nenc.payload = msg;\nenc.key(key);\nenc.secure_content(null);\nlet bytes = enc.encode(true);\n```\n\n### Decode cose-encrypt0\n```js\nvar {\n  CoseKey,\n  CoseMessage,\n  Alg,\n  Kty,\n  KeyOp\n} = require('cose-w');\nvar assert = require('assert');\n\n// \"This is the content.\"\nlet expected_msg = Buffer.from('546869732069732074686520636F6E74656E742E', 'hex');\n\n// Prepare cose-key\nlet key = new CoseKey();\nkey.kty = Kty.symmetric;\nkey.alg = Alg.chacha20;\nkey.k = Buffer.from('849b57219dae48de646d07dbb533566e976686457c1491be3a76dcea6c427188', 'hex');\nkey.key_ops = [KeyOp.decrypt];\n\n// Prepare CoseEncrypt with the cose-encrypt0 bytes\nlet dec = CoseMessage.new_encrypt();\ndec.bytes = Buffer.from(\"d08352a2011818054c89f52f65a1c580933b5261a7a0582481c32c048134989007b3b5b932811ea410eeab15bd0de5d5ac5be03c84dce8c88871d6e9\", \"hex\");\n\n// Init decoding and add key\ndec.init_decoder(null);\ndec.key(key);\n\n// Decrypt and verify cose-encrypt0 message\nlet msg = dec.decode(null);\nassert.deepEqual(new Buffer.from(msg).toString(), expected_msg.toString());\n```\n\n## cose-mac0\n\nEncode/decode cose-mac0.\n\n### Encode cose-mac0\n```js\nvar {\n  CoseKey,\n  CoseHeader,\n  CoseMessage,\n  Alg,\n  Kty,\n  KeyOp\n} = require('cose-w');\n\n// Message to MAC, \"This is the content.\"\nlet msg = Buffer.from('546869732069732074686520636F6E74656E742E', 'hex');\n\n// Prepare cose-key\nlet key = new CoseKey();\nkey.kty = Kty.symmetric;\nkey.alg = Alg.aes_mac_256_128;\nkey.k = Buffer.from('849b57219dae48de646d07dbb533566e976686457c1491be3a76dcea6c427188', 'hex');\nkey.key_ops = [KeyOp.mac];\n\n// Prepare cose-mac0 header\nlet header = new CoseHeader();\nheader.set_alg(Alg.aes_mac_256_128, true, false);\n\n// Generate CoseMAC and encode the cose-mac0 final message\nlet mac = CoseMessage.new_mac();\nmac.header = header;\nmac.payload = msg;\nmac.key(key);\nmac.secure_content(null);\nlet bytes = mac.encode(true);\n```\n### Decode cose-mac0\n```js\nvar {\n  CoseKey,\n  CoseMessage,\n  Alg,\n  Kty,\n  KeyOp\n} = require('cose-w');\n\n// Prepare cose-key\nlet key = new CoseKey();\nkey.kty = Kty.symmetric;\nkey.alg = Alg.aes_mac_256_128;\nkey.k = Buffer.from('849b57219dae48de646d07dbb533566e976686457c1491be3a76dcea6c427188', 'hex');\nkey.key_ops = [KeyOp.mac_verify];\n\n// Prepare CoseMAC with the cose-mac0 bytes\nlet verify = CoseMessage.new_mac();\nverify.bytes = Buffer.from(\"d18444a101181aa054546869732069732074686520636f6e74656e742e50403152cc208c1d501e1dc2a789ae49e4\", \"hex\");\n\n// Init decoding and add key\nverify.init_decoder(null);\nverify.key(key);\n\n// Verify cose-mac0 tag\nlet payload = verify.decode(null);\n```\n\n# License\n\nThis project, cose-w, is licensed by the MIT License.\n\n# Note\n\nThis project is under development and it has not been tested yet.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftramires%2Fcose-w","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftramires%2Fcose-w","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftramires%2Fcose-w/lists"}