Class: CryptoToolchain::Tools::DSARecoverPrivateKeyFromNonce
- Defined in:
- lib/crypto_toolchain/tools/dsa_recover_private_key_from_nonce.rb
Instance Attribute Summary collapse
-
#g ⇒ Object
readonly
Returns the value of attribute g.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#p ⇒ Object
readonly
Returns the value of attribute p.
-
#public_key ⇒ Object
readonly
Returns the value of attribute public_key.
-
#q ⇒ Object
readonly
Returns the value of attribute q.
-
#r ⇒ Object
readonly
Returns the value of attribute r.
-
#s ⇒ Object
readonly
Returns the value of attribute s.
Instance Method Summary collapse
- #execute(min: 1, max: 0xffffffff) ⇒ Object
-
#initialize(public_key:, message:, r:, s:, p: DSA_P, q: DSA_Q, g: DSA_G) ⇒ DSARecoverPrivateKeyFromNonce
constructor
A new instance of DSARecoverPrivateKeyFromNonce.
- #private_key_from(k:) ⇒ Object
- #valid_k?(k) ⇒ Boolean
Constructor Details
#initialize(public_key:, message:, r:, s:, p: DSA_P, q: DSA_Q, g: DSA_G) ⇒ DSARecoverPrivateKeyFromNonce
Returns a new instance of DSARecoverPrivateKeyFromNonce.
4 5 6 7 8 9 10 11 12 |
# File 'lib/crypto_toolchain/tools/dsa_recover_private_key_from_nonce.rb', line 4 def initialize(public_key: , message: , r: , s: , p: DSA_P, q: DSA_Q, g: DSA_G) @public_key = numberize(public_key) @p = p @q = q @g = g @r = numberize(r) @s = numberize(s) @message = end |
Instance Attribute Details
#g ⇒ Object (readonly)
Returns the value of attribute g.
14 15 16 |
# File 'lib/crypto_toolchain/tools/dsa_recover_private_key_from_nonce.rb', line 14 def g @g end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
14 15 16 |
# File 'lib/crypto_toolchain/tools/dsa_recover_private_key_from_nonce.rb', line 14 def @message end |
#p ⇒ Object (readonly)
Returns the value of attribute p.
14 15 16 |
# File 'lib/crypto_toolchain/tools/dsa_recover_private_key_from_nonce.rb', line 14 def p @p end |
#public_key ⇒ Object (readonly)
Returns the value of attribute public_key.
14 15 16 |
# File 'lib/crypto_toolchain/tools/dsa_recover_private_key_from_nonce.rb', line 14 def public_key @public_key end |
#q ⇒ Object (readonly)
Returns the value of attribute q.
14 15 16 |
# File 'lib/crypto_toolchain/tools/dsa_recover_private_key_from_nonce.rb', line 14 def q @q end |
#r ⇒ Object (readonly)
Returns the value of attribute r.
14 15 16 |
# File 'lib/crypto_toolchain/tools/dsa_recover_private_key_from_nonce.rb', line 14 def r @r end |
#s ⇒ Object (readonly)
Returns the value of attribute s.
14 15 16 |
# File 'lib/crypto_toolchain/tools/dsa_recover_private_key_from_nonce.rb', line 14 def s @s end |
Instance Method Details
#execute(min: 1, max: 0xffffffff) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/crypto_toolchain/tools/dsa_recover_private_key_from_nonce.rb', line 31 def execute(min: 1, max: 0xffffffff) (min..max).each do |k| return private_key_from(k: k) if valid_k?(k) end raise RuntimeError.new("Could not recover key") end |
#private_key_from(k:) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/crypto_toolchain/tools/dsa_recover_private_key_from_nonce.rb', line 22 def private_key_from(k: ) # (s * k) - H(msg) # x = ---------------- mod q # r numerator = ((s * k) - CryptoToolchain::Utilities::SHA1.digest().to_number) % q denominator = r.invmod(q) ((numerator * denominator) % q).to_bin_string end |
#valid_k?(k) ⇒ Boolean
16 17 18 19 20 |
# File 'lib/crypto_toolchain/tools/dsa_recover_private_key_from_nonce.rb', line 16 def valid_k?(k) x = private_key_from(k: k) kp = CryptoToolchain::BlackBoxes::DSAKeypair.new(p: p, q: q, g: g, private_key: x) kp.public_key == public_key end |