Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jwt/ruby-jwe
JSON Web Encryption for Ruby
https://github.com/jwt/ruby-jwe
Last synced: 3 months ago
JSON representation
JSON Web Encryption for Ruby
- Host: GitHub
- URL: https://github.com/jwt/ruby-jwe
- Owner: jwt
- License: mit
- Created: 2016-01-10T09:12:27.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2024-01-24T10:04:30.000Z (9 months ago)
- Last Synced: 2024-07-14T20:43:11.612Z (4 months ago)
- Language: Ruby
- Size: 57.6 KB
- Stars: 64
- Watchers: 6
- Forks: 14
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# JWE
[![Build Status](https://travis-ci.org/jwt/ruby-jwe.svg)](https://travis-ci.org/jwt/ruby-jwe)
[![Code Climate](https://codeclimate.com/github/jwt/ruby-jwe/badges/gpa.svg)](https://codeclimate.com/github/jwt/ruby-jwe)
[![Test Coverage](https://codeclimate.com/github/jwt/ruby-jwe/badges/coverage.svg)](https://codeclimate.com/github/aomega08/jwe/coverage)A ruby implementation of the [RFC 7516 JSON Web Encryption (JWE)](https://tools.ietf.org/html/rfc7516) standard.
## Installing
```bash
gem install jwe
```
## UsageThis example uses the default alg and enc methods (RSA-OAEP and A128CBC-HS256). It requires an RSA key.
```ruby
require 'jwe'key = OpenSSL::PKey::RSA.generate(2048)
payload = "The quick brown fox jumps over the lazy dog."encrypted = JWE.encrypt(payload, key)
puts encryptedplaintext = JWE.decrypt(encrypted, key)
puts plaintext #"The quick brown fox jumps over the lazy dog."
```This example uses a custom enc method:
```ruby
require 'jwe'key = OpenSSL::PKey::RSA.generate(2048)
payload = "The quick brown fox jumps over the lazy dog."encrypted = JWE.encrypt(payload, key, enc: 'A192GCM')
puts encryptedplaintext = JWE.decrypt(encrypted, key)
puts plaintext #"The quick brown fox jumps over the lazy dog."
```This example uses the 'dir' alg method. It requires an encryption key of the correct size for the enc method
```ruby
require 'jwe'key = SecureRandom.random_bytes(32)
payload = "The quick brown fox jumps over the lazy dog."encrypted = JWE.encrypt(payload, key, alg: 'dir')
puts encryptedplaintext = JWE.decrypt(encrypted, key)
puts plaintext #"The quick brown fox jumps over the lazy dog."
```This example uses the DEFLATE algorithm on the plaintext to reduce the result size.
```ruby
require 'jwe'key = OpenSSL::PKey::RSA.generate(2048)
payload = "The quick brown fox jumps over the lazy dog."encrypted = JWE.encrypt(payload, key, zip: 'DEF')
puts encryptedplaintext = JWE.decrypt(encrypted, key)
puts plaintext #"The quick brown fox jumps over the lazy dog."
```This example sets an extra **plaintext** custom header.
```ruby
require 'jwe'key = OpenSSL::PKey::RSA.generate(2048)
payload = "The quick brown fox jumps over the lazy dog."# In this case we add a copyright line to the headers (it can be anything you like
# just remember it is plaintext).
encrypted = JWE.encrypt(payload, key, copyright: 'This is my stuff! All rights reserved')
puts encrypted
```## Available Algorithms
The RFC 7518 JSON Web Algorithms (JWA) spec defines the algorithms for [encryption](https://tools.ietf.org/html/rfc7518#section-5.1)
and [key management](https://tools.ietf.org/html/rfc7518#section-4.1) to be supported by a JWE implementation.Only a subset of these algorithms is implemented in this gem. Striked elements are not available:
Key management:
* RSA1_5
* RSA-OAEP (default)
* ~~RSA-OAEP-256~~
* A128KW
* A192KW
* A256KW
* dir
* ~~ECDH-ES~~
* ~~ECDH-ES+A128KW~~
* ~~ECDH-ES+A192KW~~
* ~~ECDH-ES+A256KW~~
* ~~A128GCMKW~~
* ~~A192GCMKW~~
* ~~A256GCMKW~~
* ~~PBES2-HS256+A128KW~~
* ~~PBES2-HS384+A192KW~~
* ~~PBES2-HS512+A256KW~~Encryption:
* A128CBC-HS256 (default)
* A192CBC-HS384
* A256CBC-HS512
* A128GCM
* A192GCM
* A256GCM## License
The MIT License
* Copyright © 2016 Francesco Boffa
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.