File: keygen.rb

package info (click to toggle)
ruby-mechanize 2.7.6-1%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,480 kB
  • sloc: ruby: 11,380; makefile: 5; sh: 4
file content (34 lines) | stat: -rw-r--r-- 984 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
##
# This class represents a keygen (public / private key generator) found in a
# Form. The field will automatically generate a key pair and compute its own
# value to match the challenge. Call key to access the public/private key
# pair.

class Mechanize::Form::Keygen < Mechanize::Form::Field
  # The challenge for this <keygen>.
  attr_reader :challenge

  # The key associated with this <keygen> tag.
  attr_reader :key

  def initialize(node, value = nil)
    super
    @challenge = node['challenge']

    @spki = OpenSSL::Netscape::SPKI.new
    @spki.challenge = @challenge

    @key = nil
    generate_key if value.nil? || value.empty?
  end

  # Generates a key pair and sets the field's value.
  def generate_key(key_size = 2048)
    # Spec at http://dev.w3.org/html5/spec/Overview.html#the-keygen-element
    @key = OpenSSL::PKey::RSA.new key_size
    @spki.public_key = @key.public_key
    @spki.sign @key, OpenSSL::Digest::MD5.new
    self.value = @spki.to_pem
  end
end