Class: Tem::Builders::Crypto

Inherits:
Abi
  • Object
show all
Defined in:
lib/tem/builders/crypto.rb

Overview

Builder class and namespace for the cryptography builder.

Defined Under Namespace

Modules: Impl

Instance Attribute Summary

Attributes inherited from Abi

#target

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Abi

#conditional_wrapper, define_abi, #fixed_length_number, #fixed_length_string, #initialize, #object_wrapper, #packed_variable_length_numbers, #variable_length_number

Constructor Details

This class inherits a constructor from Tem::Builders::Abi

Class Method Details

.define_crypto(class_or_module) {|new(class_or_module)| ... } ⇒ Object

Creates a builder targeting a module / class.

The given parameter should be a class or module

Yields:

  • (new(class_or_module))


18
19
20
# File 'lib/tem/builders/crypto.rb', line 18

def self.define_crypto(class_or_module)  # :yields: crypto
  yield new(class_or_module)
end

Instance Method Details

#asymmetric_key(name, ssl_class, privkey_abi_type, pubkey_abi_type, hooks = {}) ⇒ Object

Defines the methods for handling an asymmetric (public/private) key.

ssl_class should be a class in OpenSSL::PKey. privkey_abi_type and pubkey_abi_type should be ABI types similar to those produced by packed_variable_length_numbers.

The following methods are defined for a type named ‘name’:

* read_private_name(array, offset) -> key
* to_private_name(key) -> array
* private_name_class -> Class
* read_public_name(array, offset) -> key
* to_public_name(key) -> array
* public_name_class -> Class


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/tem/builders/crypto.rb', line 35

def asymmetric_key(name, ssl_class, privkey_abi_type, pubkey_abi_type,
                   hooks = {})
  object_wrapper "private_#{name}", Tem::Keys::Asymmetric,
                 [privkey_abi_type, nil],
                 :read => hooks[:read_private] || hooks[:read] ||
                          lambda { |k| Tem::Keys::Asymmetric.new k },
                 :to => hooks[:to_private] || hooks[:to] ||
                        lambda { |k| k.ssl_key },
                 :new => hooks[:new_private] || hooks[:new] ||
                         lambda { |k| ssl_class.new }
  object_wrapper "public_#{name}", Tem::Keys::Asymmetric,
                 [pubkey_abi_type, nil],
                 :read => hooks[:read_public] || hooks[:read] ||
                          lambda { |k| Tem::Keys::Asymmetric.new k },                   
                 :to => hooks[:to_public] || hooks[:to] ||
                        lambda { |k| k.ssl_key },
                 :new => hooks[:new_private] || hooks[:new] ||
                         lambda { |k| ssl_class.new }
end

#crypto_hash(name, digest_class) ⇒ Object

Defines the methods for a cryptographic hash function.

digest_class should be an object similar to the classes in the Digest name-space. Specifically, it should implement the digest method.

The following methods are defined for a type named ‘name’:

* name(array | String) -> array
* name_length -> number
* name_digest_class -> Class


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/tem/builders/crypto.rb', line 98

def crypto_hash(name, digest_class)
  digest_length = digest_class.digest('').length

  defines = Proc.new do 
    define_method :"#{name}" do |data|
      data = data.pack 'C*' unless data.kind_of? String
      digest_class.digest(data).unpack 'C*'
    end
    define_method(:"#{name}_digest_class") { digest_class }
    define_method(:"#{name}_length") { digest_length }
  end
  
  @target.class_eval(&defines)
  (class << @target; self; end).module_eval(&defines)    
end

#symmetric_key(name, cipher_class, cipher_name, key_abi_type, hooks = {}) ⇒ Object

Defines the methods for a symmetric key.

cipher_class should be a class in OpenSSL::Cipher. key_abi_type should be an ABI type similar to that produced by fixed_string.

The following methods are defined for a type named ‘name’:

* read_name(array, offset) -> object
* to_name(object) -> array
* name_class -> Class


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/tem/builders/crypto.rb', line 65

def symmetric_key(name, cipher_class, cipher_name, key_abi_type, hooks = {})
  object_wrapper name, Tem::Keys::Symmetric, [key_abi_type, :key],
      :read => lambda { |k| Tem::Keys::Symmetric.new k },
      :to => lambda { |k| k.ssl_key },
      :new => lambda { |klass|
    k = cipher_class.new cipher_name
    
    unless k.respond_to? :key
      # Some ciphers don't give back the key that they receive.
      # We need to synthesize that.
      class <<k
        def key=(new_key)
          super
          @_key = new_key
        end
        def key
          @_key
        end
      end
    end
    k
  }
end