Line | |
---|
1 | =begin
|
---|
2 | = $RCSfile$ -- Ruby-space predefined Cipher subclasses
|
---|
3 |
|
---|
4 | = Info
|
---|
5 | 'OpenSSL for Ruby 2' project
|
---|
6 | Copyright (C) 2002 Michal Rokos <[email protected]>
|
---|
7 | All rights reserved.
|
---|
8 |
|
---|
9 | = Licence
|
---|
10 | This program is licenced under the same licence as Ruby.
|
---|
11 | (See the file 'LICENCE'.)
|
---|
12 |
|
---|
13 | = Version
|
---|
14 | $Id: cipher.rb 11708 2007-02-12 23:01:19Z shyouhei $
|
---|
15 | =end
|
---|
16 |
|
---|
17 | ##
|
---|
18 | # Should we care what if somebody require this file directly?
|
---|
19 | #require 'openssl'
|
---|
20 |
|
---|
21 | module OpenSSL
|
---|
22 | module Cipher
|
---|
23 | %w(AES CAST5 BF DES IDEA RC2 RC4 RC5).each{|name|
|
---|
24 | klass = Class.new(Cipher){
|
---|
25 | define_method(:initialize){|*args|
|
---|
26 | cipher_name = args.inject(name){|n, arg| "#{n}-#{arg}" }
|
---|
27 | super(cipher_name)
|
---|
28 | }
|
---|
29 | }
|
---|
30 | const_set(name, klass)
|
---|
31 | }
|
---|
32 |
|
---|
33 | %w(128 192 256).each{|keylen|
|
---|
34 | klass = Class.new(Cipher){
|
---|
35 | define_method(:initialize){|mode|
|
---|
36 | mode ||= "CBC"
|
---|
37 | cipher_name = "AES-#{keylen}-#{mode}"
|
---|
38 | super(cipher_name)
|
---|
39 | }
|
---|
40 | }
|
---|
41 | const_set("AES#{keylen}", klass)
|
---|
42 | }
|
---|
43 |
|
---|
44 | class Cipher
|
---|
45 | def random_key
|
---|
46 | str = OpenSSL::Random.random_bytes(self.key_len)
|
---|
47 | self.key = str
|
---|
48 | return str
|
---|
49 | end
|
---|
50 |
|
---|
51 | def random_iv
|
---|
52 | str = OpenSSL::Random.random_bytes(self.iv_len)
|
---|
53 | self.iv = str
|
---|
54 | return str
|
---|
55 | end
|
---|
56 | end
|
---|
57 | end # Cipher
|
---|
58 | end # OpenSSL
|
---|
Note:
See
TracBrowser
for help on using the repository browser.