<commit_before>module Certmeister

  class SelfTest

    # Pass in PEM-encoded key for fast tests that don't need lots of entropy.
    def initialize(ca, key = nil)
      @ca = ca
      @key = key
    end

    def test(req = {cn: 'test', ip: '127.0.0.1'})
      begin
        test!(req = {cn: 'test', ip: '127.0.0.1'})
        Result.new(true, {message: "OK"})
      rescue Exception => e
        Result.new(false, {message: e.message})
      end
    end

    def test!(req = {cn: 'test', ip: '127.0.0.1'})
      res = @ca.remove(req)
      res.hit? or res.miss? or raise "Test certificate remove failed: #{res.error}"

      csr = get_csr("C=ZA, ST=Western Cape, L=Cape Town, O=Hetzner PTY Ltd, CN=#{req[:cn]}")
      res = @ca.sign(cn: 'test', csr: csr.to_pem, ip: '127.0.0.1')
      res.hit? or raise "Test certificate signing failed: #{res.error}"

      res = @ca.fetch(cn: 'test', ip: '127.0.0.1')
      res.hit? or raise "Test certificate fetch failed: #{res.error}"

      cert = OpenSSL::X509::Certificate.new(res.pem)
      cert.subject.to_s =~ /CN=#{req[:cn]}/ or raise "Test certificate common name mismatch"

      nil
    end

    private

    def get_csr(subject)
      key = get_key
      csr = OpenSSL::X509::Request.new
      csr.version = 0
      csr.subject = OpenSSL::X509::Name.parse(subject)
      csr.public_key = key.public_key
      csr.sign key, OpenSSL::Digest::SHA1.new
      csr
    end

    def get_key
      OpenSSL::PKey::RSA.new(@key || 4096).tap do |key|
        @key ||= key.to_pem
      end
    end

    class Result
      attr_reader :data

      def initialize(ok, data)
        @ok = !!ok
        @data = data
      end

      def ok?
        @ok
      end

      def message
        @data.fetch(:message, nil) if @data.respond_to?(:fetch)
      end

    end

  end

end
<commit_msg>Use SH256 signature in self test CSR<commit_after>module Certmeister

  class SelfTest

    # Pass in PEM-encoded key for fast tests that don't need lots of entropy.
    def initialize(ca, key = nil)
      @ca = ca
      @key = key
    end

    def test(req = {cn: 'test', ip: '127.0.0.1'})
      begin
        test!(req = {cn: 'test', ip: '127.0.0.1'})
        Result.new(true, {message: "OK"})
      rescue Exception => e
        Result.new(false, {message: e.message})
      end
    end

    def test!(req = {cn: 'test', ip: '127.0.0.1'})
      res = @ca.remove(req)
      res.hit? or res.miss? or raise "Test certificate remove failed: #{res.error}"

      csr = get_csr("C=ZA, ST=Western Cape, L=Cape Town, O=Hetzner PTY Ltd, CN=#{req[:cn]}")
      res = @ca.sign(cn: 'test', csr: csr.to_pem, ip: '127.0.0.1')
      res.hit? or raise "Test certificate signing failed: #{res.error}"

      res = @ca.fetch(cn: 'test', ip: '127.0.0.1')
      res.hit? or raise "Test certificate fetch failed: #{res.error}"

      cert = OpenSSL::X509::Certificate.new(res.pem)
      cert.subject.to_s =~ /CN=#{req[:cn]}/ or raise "Test certificate common name mismatch"

      nil
    end

    private

    def get_csr(subject)
      key = get_key
      csr = OpenSSL::X509::Request.new
      csr.version = 0
      csr.subject = OpenSSL::X509::Name.parse(subject)
      csr.public_key = key.public_key
      csr.sign key, OpenSSL::Digest::SHA256.new
      csr
    end

    def get_key
      OpenSSL::PKey::RSA.new(@key || 4096).tap do |key|
        @key ||= key.to_pem
      end
    end

    class Result
      attr_reader :data

      def initialize(ok, data)
        @ok = !!ok
        @data = data
      end

      def ok?
        @ok
      end

      def message
        @data.fetch(:message, nil) if @data.respond_to?(:fetch)
      end

    end

  end

end

{'fraction_non_alphanumeric': 0.14383001906837373, 'fraction_numerical': 0.02587850721874149, 'mean_word_length': 2.1847354726799653, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 10, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5258891', 'n_tokens_mistral': 1393, 'n_tokens_neox': 1300, 'n_words': 350}