Typescript COSE implementation
A modern TypeScript implementation of COSE (CBOR Object Signing and Encryption) as defined in RFC 8152.
- ๐ Complete COSE implementation - Support for signing, MAC, and encryption operations
- ๐ Browser and Node.js compatible - Works in both environments with proper polyfills
- ๐ฏ TypeScript first - Fully typed API with comprehensive type definitions
- ๐ก๏ธ Modern cryptography - Built on @noble cryptographic libraries
- ๐งช Well tested - Comprehensive test suite with Vitest
- ๐ฆ Multiple formats - ESM, UMD, and CommonJS support
``bash`
npm install @lukachi/cose-ts
`typescript
import { sign } from '@lukachi/cose-ts';
// Create a signed message
const payload = Buffer.from('Hello, COSE!');
const headers = {
p: { alg: 'ES256' },
u: { kid: 'my-key-id' }
};
const signer = {
key: {
kty: 'EC2',
crv: 'P-256',
d: Buffer.from('private-key-bytes', 'hex'),
x: Buffer.from('public-key-x-coordinate', 'hex'),
y: Buffer.from('public-key-y-coordinate', 'hex')
}
};
const signedMessage = await sign.create(headers, payload, signer);
// Verify the signature
const verifier = {
key: {
kty: 'EC2',
crv: 'P-256',
x: Buffer.from('public-key-x-coordinate', 'hex'),
y: Buffer.from('public-key-y-coordinate', 'hex')
}
};
const verifiedPayload = await sign.verify(signedMessage, verifier);
`
`typescript
import { mac } from '@lukachi/cose-ts';
// Create a MAC
const payload = Buffer.from('Important message!');
const headers = {
p: { alg: 'HS256' },
u: { kid: 'shared-secret-id' }
};
const recipient = {
key: Buffer.from('shared-secret-key', 'hex')
};
const macMessage = await mac.create(headers, payload, recipient);
// Verify the MAC
const verifiedPayload = await mac.read(macMessage, recipient.key);
`
`typescript
import { encrypt } from '@lukachi/cose-ts';
// Encrypt a message
const plaintext = Buffer.from('Secret message!');
const headers = {
p: { alg: 'A256GCM' },
u: { kid: 'encryption-key-id' }
};
const recipient = {
key: Buffer.from('encryption-key', 'hex')
};
const encryptedMessage = await encrypt.create(headers, plaintext, recipient);
// Decrypt the message
const decryptedPayload = await encrypt.read(encryptedMessage, recipient.key);
`
`bashClone the repository
git clone https://github.com/lukachi/cose-ts.git
cd cose-ts
$3
`
src/
โโโ index.ts # Main exports
โโโ types.ts # TypeScript type definitions
โโโ common.ts # Common utilities and constants
โโโ sign.ts # COSE_Sign1 implementation
โโโ mac.ts # COSE_Mac implementation
โโโ encrypt.ts # COSE_Encrypt implementation
โโโ cbor-utils.ts # CBOR encoding/decoding utilitiestests/ # Comprehensive test suite
example/ # React browser example application
``This library works in modern browsers with proper polyfills. The example application demonstrates browser usage with Vite providing the necessary polyfills for Node.js APIs.
For detailed API documentation and advanced usage examples, see the example application which includes interactive demonstrations of all major features.
Contributions are welcome! Please feel free to submit a Pull Request. Make sure to:
1. Add tests for new features
2. Update documentation as needed
3. Follow the existing code style
4. Ensure all tests pass
MIT License - see LICENSE file for details.
- RFC 8152 - CBOR Object Signing and Encryption (COSE)
- RFC 7049 - Concise Binary Object Representation (CBOR)