This project is currently stable, but will no longer be maintained.
Encryption
==========
[![Gem Version](https://badge.fury.io/rb/encryption.png)](http://badge.fury.io/rb/encryption)
[![Code Climate](https://codeclimate.com/github/Itehnological/encryption.png)](https://codeclimate.com/github/Itehnological/encryption)
[![Build Status](https://travis-ci.org/itay-grudev/encryption.png?branch=master)](https://travis-ci.org/itay-grudev/encryption)
A simple to use wrapper of the Ruby OpenSSL Cipher library for Ruby and Rails applications.
This gem provides an easy to use interface for symmetrical and asymmetrical encryption using RSA.
Documentation
-------------
Additional documentation and class reference can be found in the [Wiki section](https://github.com/itay-grudev/encryption/wiki) of the repository.
Installation
------------
Install the gem
```bash
gem install encryption
```
or add it to your `Gemfile`
```ruby
gem 'encryption'
```
## Symmetric encryption
### Using the global instance of the Encryption class
A simple example of how this gem works:
```ruby
Encryption.key = 'A very long encryption key'
data = 'secret data'
encrypted_str = Encryption.encrypt( data )
Encryption.decrypt( encrypted_str ) == data # true
```
### Using your own instance of the Encryption class
If you need a separate instance with custom settings, different than the global Encryption instance, here is how you can do it:
```ruby
encryptor = Encryption::Symmetric.new
encryptor.key = 'A very long encryption key'
data = 'secret data'
encrypted_str = encryptor.encrypt( data )
encryptor.decrypt( encrypted_str ) == data # true
```
### Configuration
For symmetric encryption/decryption you need to set an encryption key. You can also optionally set an initialization vector and a cipher.
`Encryption.key` - Your encryption key
`Encryption.iv # Optional` - Encryption initialization vector. Defaults to the character `"\0"` _(Optional)_
`Encryption.cipher # Optional` - Your encryption algorithm. Defaults to `aes-256-cbc` _(Optional)_
Running `openssl list-cipher-commands` in the terminal or calling `OpenSSL::Cipher.ciphers` in _Ruby_, which list all available ciphers.
You can configure both the global instance and a other instances with a _block_ like this:
```ruby
Encryption.config do |e|
e.key = 'Don't look!'
e.iv = 'This is probably the easiest way to use OpenSSL in Ruby'
e.cipher = 'camellia-128-ecb' # if you're feeling adventurous
end
```
## Asymmetric encryption (public/private key encryption)
The `encryption` gem also provides a DSL for asymmetric encryption.
### Generating keypair
```ruby
keypair = Encryption::Keypair.new # Accepts two optional arguments: size = 2048, password = nil
keypair.public_key # Instance of Encryption::PublicKey
keypair.private_key # Instance of Encryption::PrivateKey
# Or you can use this shorter version
public_key, private_key = Encryption::Keypair.generate( 2048 )
# You can dump keys to string
private_key.to_s
# or export them to PEM format
private_key.to_pem
# and optionally encrypt them with a passphrase
private_key.to_pem( 'passphrase' )
```
### `Encryption::PublicKey` and `Encryption::PrivateKey`
Both classes have the same methods:
```ruby
# Import an existing key
Encryption::PrivateKey.new( filename[, password] ) # Import from file
Encryption::PrivateKey.new( string[, password] ) # Import from string
# Encrypt / Decrypt data
public_key = Encryption::PublicKey.new( 'existing key' )
public_key.encrypt( 'some secret data' )
public_key.decrypt( "some encrypted data" )
```
_Note: You can use both the public and the private key to encrypt or decrypt data._
## Helpers
### String helper
The gem adds the `encrypt` and `decrypt` methods to the `String` performing symmetric encryption. You can use them as follows:
```ruby
# With the global Encryption instance
'Hello'.encrypt
'Hello'.encrypt!
'h3LL0'.decrypt
'h3LL0'.decrypt!
# With custom settings (and custom encryptor instance)
'secret'.encrypt( key: 'encryption key', iv: 'initialization vector', cipher: 'encryption cipher', encode: true )
# Note the encode option which will result in a base64 encoded string
'encrypted data'.decrypt( encoded: true ) # Will decrypt a base64 encoded string
# Or with a custom encryptor
encryptor = Encryption::Symmetric.new
encryptor.key = 'encryption key'
'secret data'.encrypt( encryptor: encryptor )
```
License
-------
This gem is distributed under The MIT License.
Author
------
Itay Grudev \<itay(at)grudev...com\>
没有合适的资源?快使用搜索试试~ 我知道了~
用于Ruby和Rails应用程序的OpenSSL密码库的简单包装器。通过Rubygems作为Gem分发。.zip
共32个文件
rb:24个
rspec:1个
rakefile:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 42 浏览量
2023-03-26
23:52:42
上传
评论
收藏 15KB ZIP 举报
温馨提示
用于Ruby和Rails应用程序的OpenSSL密码库的简单包装器。通过Rubygems作为Gem分发。.zip
资源推荐
资源详情
资源评论
收起资源包目录
用于Ruby和Rails应用程序的OpenSSL密码库的简单包装器。通过Rubygems作为Gem分发。.zip (32个子文件)
encryption-master
lib
configuration
base.rb 748B
symmetric.rb 289B
modules
encryptor.rb 61B
asymmetric.rb 167B
symmetric.rb 1007B
asymmetric
keypair.rb 606B
private_key.rb 201B
public_key.rb 198B
pkey.rb 547B
modules.rb 120B
helpers
string.rb 879B
encryption.rb 679B
helpers.rb 36B
configuration.rb 86B
Rakefile 119B
.rspec 25B
.travis.yml 70B
spec
configuration
symmetric_spec.rb 945B
base_spec.rb 110B
helpers
string_spec.rb 2KB
encryption
symmetric_global_spec.rb 1KB
symmetric_instance_spec.rb 1KB
asymmetric
integration_spec.rb 594B
private_key_spec.rb 609B
keypair_spec.rb 580B
public_key_spec.rb 606B
spec_helper.rb 169B
encryption.gemspec 570B
Gemfile 123B
.gitignore 56B
MIT-LICENSE 1KB
README.md 4KB
共 32 条
- 1
资源评论
快撑死的鱼
- 粉丝: 1w+
- 资源: 9154
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功