https://github.com/attr-encrypted/encryptor
A simple wrapper for the standard ruby OpenSSL library
https://github.com/attr-encrypted/encryptor
Keywords from Contributors
activejob activerecord mvc
Last synced: about 5 hours ago
JSON representation
Repository metadata
A simple wrapper for the standard ruby OpenSSL library
- Host: GitHub
- URL: https://github.com/attr-encrypted/encryptor
- Owner: attr-encrypted
- License: mit
- Created: 2009-01-07T03:43:44.000Z (almost 17 years ago)
- Default Branch: master
- Last Pushed: 2025-10-12T03:07:46.000Z (2 months ago)
- Last Synced: 2025-11-27T15:08:48.460Z (14 days ago)
- Language: Ruby
- Homepage:
- Size: 87.9 KB
- Stars: 338
- Watchers: 5
- Forks: 50
- Open Issues: 8
- Releases: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: MIT-LICENSE
README.md
Encryptor
A simple wrapper for the standard Ruby OpenSSL library
Upgrading from v2.0.0 to v3.0.0
A bug was discovered in Encryptor 2.0.0 wherein the IV was not being used when using an AES-*-GCM algorithm. Unfornately fixing this major security issue results in the inability to decrypt records encrypted using an AES-*-GCM algorithm from Encryptor v2.0.0. While the behavior change is minimal between v2.0.0 and v3.0.0, the change has a significant impact on users that used v2.0.0 and encrypted data using an AES-*-GCM algorithm, which is the default algorithm for v2.0.0. Consequently, we decided to increment the version with a major bump to help people avoid a confusing situation where some of their data will not decrypt. A new option is available in Encryptor 3.0.0 that allows decryption of data encrypted using an AES-*-GCM algorithm from Encryptor v2.0.0.
Installation
gem install encryptor
Usage
Basic
Encryptor uses the AES-256-GCM algorithm by default to encrypt strings securely.
The best example is:
cipher = OpenSSL::Cipher.new('aes-256-gcm')
cipher.encrypt # Required before '#random_key' or '#random_iv' can be called. http://ruby-doc.org/stdlib-2.0.0/libdoc/openssl/rdoc/OpenSSL/Cipher.html#method-i-encrypt
secret_key = cipher.random_key # Insures that the key is the correct length respective to the algorithm used.
iv = cipher.random_iv # Insures that the IV is the correct length respective to the algorithm used.
salt = SecureRandom.random_bytes(16)
encrypted_value = Encryptor.encrypt(value: 'some string to encrypt', key: secret_key, iv: iv, salt: salt)
decrypted_value = Encryptor.decrypt(value: encrypted_value, key: secret_key, iv: iv, salt: salt)
A slightly easier example is:
require 'securerandom'
secret_key = SecureRandom.random_bytes(32) # The length in bytes must be equal to or greater than the algorithm bit length.
iv = SecureRandom.random_bytes(12) # Recommended length for AES-###-GCM algorithm. https://tools.ietf.org/html/rfc5084#section-3.2
encrypted_value = Encryptor.encrypt(value: 'some string to encrypt', key: secret_key, iv: iv)
decrypted_value = Encryptor.decrypt(value: encrypted_value, key: secret_key, iv: iv)
NOTE: It is imperative that you use a unique IV per each string and encryption key combo; a nonce as the IV.
See RFC 5084 for more details.
The value to encrypt or decrypt may also be passed as the first option if you'd prefer.
encrypted_value = Encryptor.encrypt('some string to encrypt', key: secret_key, iv: iv)
decrypted_value = Encryptor.decrypt(encrypted_value, key: secret_key, iv: iv)
Options
Defaults:
{ algorithm: 'aes-256-gcm',
auth_data: '',
insecure_mode: false,
hmac_iterations: 2000,
v2_gcm_iv: false }
Older versions of Encryptor allowed you to use it in a less secure way. Namely, you were allowed to run Encryptor without an IV, or with a key of insufficient length. Encryptor now requires a key and IV of the correct length respective to the algorithm that you use. However, to maintain backwards compatibility you can run Encryptor with the :insecure_mode option. Additionally, when using AES-*-GCM algorithms in Encryptor v2.0.0, the IV was set incorrectly and was not used. The :v2_gcm_iv option is available to allow Encryptor to set the IV as it was set in Encryptor v2.0.0. This is provided to assist with migrating data that unsafely encrypted using an AES-*-GCM algorithm from Encryptor v2.0.0.
You may also pass an :algorithm,:salt, and hmac_iterations option, however none of these options are required. If you pass the :salt option, a new unique key will be derived from the key that you passed in using PKCS5 with a default of 2000 iterations. You can change the number of PKCS5 iterations with the hmac_iterations option. As PKCS5 is slow, it is optional behavior, but it does provide more security to use a unique IV and key for every encryption operation.
Encryptor.default_options.merge!(algorithm: 'aes-256-cbc', key: 'some default secret key', iv: iv, salt: salt)
Strings
Older versions of Encryptor added encrypt and decrypt methods to String objects for your convenience. However, this behavior has been removed to avoid polluting Ruby's core String class. The Encryptor::String module remains within this gem to allow users of this feature to implement it themselves. These encrypt and decrypt methods accept the same arguments as the associated ones in the Encryptor module. They're nice when you set the default options in the Encryptor.default_options attribute. For example:
require 'encryptor/string'
String.include Encryptor::String
Encryptor.default_options.merge!(key: 'some default secret key', iv: iv)
credit_card = 'xxxx xxxx xxxx 1234'
encrypted_credit_card = credit_card.encrypt
There's also encrypt! and decrypt! methods that replace the contents of a string with the encrypted or decrypted version of itself.
Algorithms
To view a list of all cipher algorithms that are supported on your platform, run the following code in your favorite Ruby REPL:
require 'openssl'
puts OpenSSL::Cipher.ciphers
The supported ciphers will vary depending on the version of OpenSSL that was used to compile your version of Ruby. However, the following ciphers are typically supported:
| Cipher Name | Key size in bytes | IV size in bytes |
|---|---|---|
| aes-128-cbc | 16 | 16 |
| aes-128-cbc-hmac-sha1 | 16 | 16 |
| aes-128-cbc-hmac-sha256 | 16 | 16 |
| aes-128-ccm | 16 | 12 |
| aes-128-cfb | 16 | 16 |
| aes-128-cfb1 | 16 | 16 |
| aes-128-cfb8 | 16 | 16 |
| aes-128-ctr | 16 | 16 |
| aes-128-ecb | 16 | 0 |
| aes-128-gcm | 16 | 12 |
| aes-128-ofb | 16 | 16 |
| aes-128-xts | 32 | 16 |
| aes-192-cbc | 24 | 16 |
| aes-192-ccm | 24 | 12 |
| aes-192-cfb | 24 | 16 |
| aes-192-cfb1 | 24 | 16 |
| aes-192-cfb8 | 24 | 16 |
| aes-192-ctr | 24 | 16 |
| aes-192-ecb | 24 | 0 |
| aes-192-gcm | 24 | 12 |
| aes-192-ofb | 24 | 16 |
| aes-256-cbc | 32 | 16 |
| aes-256-cbc-hmac-sha1 | 32 | 16 |
| aes-256-cbc-hmac-sha256 | 32 | 16 |
| aes-256-ccm | 32 | 12 |
| aes-256-cfb | 32 | 16 |
| aes-256-cfb1 | 32 | 16 |
| aes-256-cfb8 | 32 | 16 |
| aes-256-ctr | 32 | 16 |
| aes-256-ecb | 32 | 0 |
| aes-256-gcm | 32 | 12 |
| aes-256-ofb | 32 | 16 |
| aes-256-xts | 64 | 16 |
| aes128 | 16 | 16 |
| aes192 | 24 | 16 |
| aes256 | 32 | 16 |
| bf | 16 | 8 |
| bf-cbc | 16 | 8 |
| bf-cfb | 16 | 8 |
| bf-ecb | 16 | 0 |
| bf-ofb | 16 | 8 |
| blowfish | 16 | 8 |
| camellia-128-cbc | 16 | 16 |
| camellia-128-cfb | 16 | 16 |
| camellia-128-cfb1 | 16 | 16 |
| camellia-128-cfb8 | 16 | 16 |
| camellia-128-ecb | 16 | 0 |
| camellia-128-ofb | 16 | 16 |
| camellia-192-cbc | 24 | 16 |
| camellia-192-cfb | 24 | 16 |
| camellia-192-cfb1 | 24 | 16 |
| camellia-192-cfb8 | 24 | 16 |
| camellia-192-ecb | 24 | 0 |
| camellia-192-ofb | 24 | 16 |
| camellia-256-cbc | 32 | 16 |
| camellia-256-cfb | 32 | 16 |
| camellia-256-cfb1 | 32 | 16 |
| camellia-256-cfb8 | 32 | 16 |
| camellia-256-ecb | 32 | 0 |
| camellia-256-ofb | 32 | 16 |
| camellia128 | 16 | 16 |
| camellia192 | 24 | 16 |
| camellia256 | 32 | 16 |
| cast | 16 | 8 |
| cast-cbc | 16 | 8 |
| cast5-cbc | 16 | 8 |
| cast5-cfb | 16 | 8 |
| cast5-ecb | 16 | 0 |
| cast5-ofb | 16 | 8 |
| des | 8 | 8 |
| des-cbc | 8 | 8 |
| des-cfb | 8 | 8 |
| des-cfb1 | 8 | 8 |
| des-cfb8 | 8 | 8 |
| des-ecb | 8 | 0 |
| des-ede | 16 | 0 |
| des-ede-cbc | 16 | 8 |
| des-ede-cfb | 16 | 8 |
| des-ede-ofb | 16 | 8 |
| des-ede3 | 24 | 0 |
| des-ede3-cbc | 24 | 8 |
| des-ede3-cfb | 24 | 8 |
| des-ede3-cfb1 | 24 | 8 |
| des-ede3-cfb8 | 24 | 8 |
| des-ede3-ofb | 24 | 8 |
| des-ofb | 8 | 8 |
| des3 | 24 | 8 |
| desx | 24 | 8 |
| desx-cbc | 24 | 8 |
| idea | 16 | 8 |
| idea-cbc | 16 | 8 |
| idea-cfb | 16 | 8 |
| idea-ecb | 16 | 0 |
| idea-ofb | 16 | 8 |
| rc2 | 16 | 8 |
| rc2-40-cbc | 5 | 8 |
| rc2-64-cbc | 8 | 8 |
| rc2-cbc | 16 | 8 |
| rc2-cfb | 16 | 8 |
| rc2-ecb | 16 | 0 |
| rc2-ofb | 16 | 8 |
| rc4 | 16 | 0 |
| rc4-40 | 5 | 0 |
| rc4-hmac-md5 | 16 | 0 |
| seed | 16 | 16 |
| seed-cbc | 16 | 16 |
| seed-cfb | 16 | 16 |
| seed-ecb | 16 | 0 |
| seed-ofb | 16 | 16 |
NOTE: Some ciphers may not be supported by Ruby. Additionally, Ruby compiled with OpenSSL >= v1.0.1 will include AEAD ciphers, ie., aes-256-gcm.
Notes on patches/pull requests
- Fork the project.
- Make your feature addition or bug fix.
- Add tests for it: this is important so I don't break it in a future version unintentionally.
- Commit, do not mess with Rakefile, version, or history: if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull).
- Send me a pull request: bonus points for topic branches.
Owner metadata
- Name: attr-encrypted
- Login: attr-encrypted
- Email:
- Kind: organization
- Description:
- Website:
- Location:
- Twitter:
- Company:
- Icon url: https://avatars.githubusercontent.com/u/5475200?v=4
- Repositories: 2
- Last ynced at: 2024-03-25T19:31:33.134Z
- Profile URL: https://github.com/attr-encrypted
GitHub Events
Total
- Watch event: 2
Last Year
Committers metadata
Last synced: 1 day ago
Total Commits: 91
Total Committers: 9
Avg Commits per committer: 10.111
Development Distribution Score (DDS): 0.549
Commits in past year: 1
Committers in past year: 1
Avg Commits per committer in past year: 1.0
Development Distribution Score (DDS) in past year: 0.0
| Name | Commits | |
|---|---|---|
| Sean Huber | s****r@h****m | 41 |
| Stephen Aghaulor | s****r@g****m | 22 |
| Richard Cook | r****k@r****g | 8 |
| Daniel Palacio | d****l@g****m | 8 |
| S. Brent Faulkner | b****f@u****t | 5 |
| S. Brent Faulkner | b****r@m****m | 3 |
| Tamir Duberstein | t****d@s****m | 2 |
| Matouš Borák | m****k@p****z | 1 |
| Edward Betts | e****d@4****m | 1 |
Committer domains:
- 4angle.com: 1
- platanus.cz: 1
- squareup.com: 1
- mosaic.com: 1
- unwwwired.net: 1
- rcook.org: 1
- huberry.com: 1
Issue and Pull Request metadata
Last synced: about 1 month ago
Total issues: 20
Total pull requests: 17
Average time to close issues: 8 months
Average time to close pull requests: over 1 year
Total issue authors: 20
Total pull request authors: 10
Average comments per issue: 2.8
Average comments per pull request: 2.71
Merged pull request: 8
Bot issues: 0
Bot pull requests: 0
Past year issues: 0
Past year pull requests: 0
Past year average time to close issues: N/A
Past year average time to close pull requests: N/A
Past year issue authors: 0
Past year pull request authors: 0
Past year average comments per issue: 0
Past year average comments per pull request: 0
Past year merged pull request: 0
Past year bot issues: 0
Past year bot pull requests: 0
Top Issue Authors
- jorgevbo (1)
- dlangevin (1)
- darrenboyd (1)
- madhusudhan518 (1)
- utkarsh2102 (1)
- cheynewallace (1)
- kylesziv (1)
- l8nite (1)
- findchris (1)
- monfresh (1)
- ghost (1)
- MattSmithASU (1)
- rcook (1)
- levolvel (1)
- gabrielg (1)
Top Pull Request Authors
- rcook (5)
- saghaulor (4)
- EdwardBetts (1)
- tamird (1)
- sbfaulkner (1)
- tpickett66 (1)
- HDauven (1)
- ghost (1)
- borama (1)
- pravi (1)
Top Issue Labels
Top Pull Request Labels
Package metadata
- Total packages: 2
-
Total downloads:
- rubygems: 223,963,615 total
- Total docker downloads: 911,138,058
- Total dependent packages: 49 (may contain duplicates)
- Total dependent repositories: 3,086 (may contain duplicates)
- Total versions: 16
- Total maintainers: 2
gem.coop: encryptor
A simple wrapper for the standard ruby OpenSSL library to encrypt and decrypt strings
- Homepage: http://github.com/attr-encrypted/encryptor
- Documentation: http://www.rubydoc.info/gems/encryptor/
- Licenses: MIT
- Latest release: 3.0.0 (published over 9 years ago)
- Last Synced: 2025-12-11T17:33:48.266Z (about 8 hours ago)
- Versions: 8
- Dependent Packages: 0
- Dependent Repositories: 0
- Downloads: 112,003,369 Total
- Docker Downloads: 455,569,029
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 0.115%
- Downloads: 0.221%
- Docker downloads count: 0.241%
- Maintainers (2)
rubygems.org: encryptor
A simple wrapper for the standard ruby OpenSSL library to encrypt and decrypt strings
- Homepage: http://github.com/attr-encrypted/encryptor
- Documentation: http://www.rubydoc.info/gems/encryptor/
- Licenses: MIT
- Latest release: 3.0.0 (published over 9 years ago)
- Last Synced: 2025-12-10T17:32:14.859Z (1 day ago)
- Versions: 8
- Dependent Packages: 49
- Dependent Repositories: 3,086
- Downloads: 111,960,246 Total
- Docker Downloads: 455,569,029
-
Rankings:
- Downloads: 0.216%
- Docker downloads count: 0.301%
- Dependent packages count: 0.539%
- Dependent repos count: 0.561%
- Average: 1.388%
- Stargazers count: 3.093%
- Forks count: 3.618%
- Maintainers (2)
Dependencies
- codeclimate-test-reporter >= 0 development
- minitest >= 0 development
- rake >= 0 development
- simplecov >= 0 development
- simplecov-rcov >= 0 development
Score: 28.89365457077588