Class: TTCrypt::RsaKey

Inherits:
Object
  • Object
show all
Defined in:
lib/ttcrypt.rb,
ext/ttcrypt/ttcrypt_ruby.cpp

Overview

Implementation of RSAES-OAEP encryption and RSASSA-PSS signing accroding to pkcs#1 v2.2 specification. Does NOT implement any previous cryptographically weak shcemes (like 1.5 signature) - go use openssl for itm but it does compromise private key.

All time consuming operations are executed releasing GVL so other threads can run in parallel in the multicore hardware.

Defined Under Namespace

Classes: Error

Constant Summary collapse

ACCEPTED_PARAMS =
%i|n e p q d|

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**params) ⇒ RsaKey

Returns a new instance of RsaKey.



95
96
97
# File 'lib/ttcrypt.rb', line 95

def initialize ** params
  set_params(params)
end

Class Method Details

.generate(bits_strength) ⇒ Object

Generate private key (that contains public key too) of the desired bit length (recommended at least 2048).



110
111
112
113
# File 'lib/ttcrypt.rb', line 110

def self.generate bits_strength
  k = RsaKey.new
  k._generate(bits_strength)
end

Instance Method Details

#bitsObject

Get key size in bits



116
117
118
# File 'lib/ttcrypt.rb', line 116

def bits
  _bits
end

#componentsObject

Get key components as hash. Components are binary strings, indexes are symbols e.g. :n, :e



172
173
174
# File 'lib/ttcrypt.rb', line 172

def components
  @components ||= _components
end

#decrypt(message) ⇒ Object

Decrypt message with private key using RSAES-OAEP scheme (pkcs#1 v.2.2). Requires private key



130
131
132
133
# File 'lib/ttcrypt.rb', line 130

def decrypt message
  message.force_encoding Encoding::BINARY
  _decrypt message
end

#eObject



189
190
191
# File 'lib/ttcrypt.rb', line 189

def e
  components[:e]
end

#encrypt(message) ⇒ Object

Encrypt message with public key using RSAES-OAEP scheme (pkcs#1 v.2.2).



122
123
124
125
# File 'lib/ttcrypt.rb', line 122

def encrypt message
  message.force_encoding Encoding::BINARY
  _encrypt message
end

#extract_publicRsaKey

Extract public key from a private (or public) key

Returns:

  • (RsaKey)

    public key instance



161
162
163
# File 'lib/ttcrypt.rb', line 161

def extract_public
  # native implementation: this is for indexing only
end

#nObject



185
186
187
# File 'lib/ttcrypt.rb', line 185

def n
  components[:n]
end

#pString

Returns P component or nil.

Returns:

  • (String)

    P component or nil



177
178
179
# File 'lib/ttcrypt.rb', line 177

def p
  components[:p]
end

#private?Boolean

true if self contains private key

Returns:

  • (Boolean)


166
167
168
# File 'lib/ttcrypt.rb', line 166

def private?
  _is_private
end

#qObject



181
182
183
# File 'lib/ttcrypt.rb', line 181

def q
  components[:q]
end

#set_params(**params) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/ttcrypt.rb', line 99

def set_params ** params
  res = {}
  params.each { |k, v|
    ACCEPTED_PARAMS.include?(k) or raise ArgumentError, "unknown key component"
    res[k.to_s] = v.to_s.force_encoding(Encoding::BINARY)
  }
  _set_params res
end

#sign(message, hash_name) ⇒ bool

Sign the message using pkcs#1 v2.2 RSASSA-PSS process. Requires private key.

Parameters:

  • message (String)

    to sign

  • hash (Symbol|String)

    function used (:sha1 or :sha256)

Returns:

  • (bool)

    true if the signature is consistent



141
142
143
144
# File 'lib/ttcrypt.rb', line 141

def sign message, hash_name
  message.force_encoding Encoding::BINARY
  _sign message, hash_name.to_s.downcase
end

#verify(message, signature, hash_name = :sha1, salt_length = 0) ⇒ bool

Check message signature signed with pkcs#1 v2.2 RSASSA-PSS process

Parameters:

  • message (String)

    to verify

  • signature (String)
  • hash (Symbol|String)

    function used (:sha1 or :sha256)

Returns:

  • (bool)

    true if the signature is consistent



153
154
155
156
157
# File 'lib/ttcrypt.rb', line 153

def verify message, signature, hash_name=:sha1, salt_length=0
  message.force_encoding Encoding::BINARY
  signature.force_encoding Encoding::BINARY
  _verify message, signature, hash_name.to_s.downcase, salt_length
end