Module: RSA::PKCS1
- Defined in:
- lib/rsa/pkcs1.rb
Overview
Support for the PKCS #1 (aka RFC 3447) padding schemes.
Class Method Summary collapse
-
.i2osp(x, len = nil) ⇒ String
Converts a nonnegative integer into an octet string of a specified length.
-
.os2ip(x) ⇒ Integer
Converts an octet string into a nonnegative integer.
-
.rsadp(k, c) ⇒ Integer
Recovers the message representative from a ciphertext representative under the control of a private key.
-
.rsaep(k, m) ⇒ Integer
Produces a ciphertext representative from a message representative under the control of a public key.
-
.rsasp1(k, m) ⇒ Integer
Produces a signature representative from a message representative under the control of a private key.
-
.rsavp1(k, s) ⇒ Integer
Recovers the message representative from a signature representative under the control of a public key.
Class Method Details
.i2osp(x, len = nil) ⇒ String
Converts a nonnegative integer into an octet string of a specified length.
This is the PKCS #1 I2OSP (Integer-to-Octet-String) primitive. Refer to PKCS #1 v2.1 pp. 8-9, section 4.1.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/rsa/pkcs1.rb', line 26 def self.i2osp(x, len = nil) raise ArgumentError, "integer too large" if len && x >= 256**len StringIO.open do |buffer| while x > 0 b = (x & 0xFF).chr x >>= 8 buffer << b end s = buffer.string s.force_encoding(Encoding::BINARY) if s.respond_to?(:force_encoding) s.reverse! s = len ? s.rjust(len, "\0") : s end end |
.os2ip(x) ⇒ Integer
Converts an octet string into a nonnegative integer.
This is the PKCS #1 OS2IP (Octet-String-to-Integer) primitive. Refer to PKCS #1 v2.1 p. 9, section 4.2.
54 55 56 |
# File 'lib/rsa/pkcs1.rb', line 54 def self.os2ip(x) x.bytes.inject(0) { |n, b| (n << 8) + b } end |
.rsadp(k, c) ⇒ Integer
Recovers the message representative from a ciphertext representative under the control of a private key.
This is the PKCS #1 RSADP decryption primitive. Refer to PKCS #1 v2.1 pp. 10-11, section 5.1.2.
88 89 90 91 92 |
# File 'lib/rsa/pkcs1.rb', line 88 def self.rsadp(k, c) n, d = k.to_a raise ArgumentError, "ciphertext representative out of range" unless c >= 0 && c < n m = Math.modpow(c, d, n) end |
.rsaep(k, m) ⇒ Integer
Produces a ciphertext representative from a message representative under the control of a public key.
This is the PKCS #1 RSAEP encryption primitive. Refer to PKCS #1 v2.1 p. 10, section 5.1.1.
70 71 72 73 74 |
# File 'lib/rsa/pkcs1.rb', line 70 def self.rsaep(k, m) n, e = k.to_a raise ArgumentError, "message representative out of range" unless m >= 0 && m < n c = Math.modpow(m, e, n) end |
.rsasp1(k, m) ⇒ Integer
Produces a signature representative from a message representative under the control of a private key.
This is the PKCS #1 RSASP1 signature primitive. Refer to PKCS #1 v2.1 pp. 12-13, section 5.2.1.
106 107 108 109 110 |
# File 'lib/rsa/pkcs1.rb', line 106 def self.rsasp1(k, m) n, d = k.to_a raise ArgumentError, "message representative out of range" unless m >= 0 && m < n s = Math.modpow(m, d, n) end |
.rsavp1(k, s) ⇒ Integer
Recovers the message representative from a signature representative under the control of a public key.
This is the PKCS #1 RSAVP1 verification primitive. Refer to PKCS #1 v2.1 p. 13, section 5.2.2.
124 125 126 127 128 |
# File 'lib/rsa/pkcs1.rb', line 124 def self.rsavp1(k, s) n, e = k.to_a raise ArgumentError, "signature representative out of range" unless s >= 0 && s < n m = Math.modpow(s, e, n) end |