非常教程

Ruby 2.4参考手册

OpenSSL

OpenSSL::Config

父类:ObjectIncluded 模块:Enumerable

openssl 库的配置。

许多系统安装 openssl 库将取决于您的系统配置。请参阅 OpenSSL :: Config :: DEFAULT_CONFIG_FILE 的值来了解主机文件的位置。

常量

DEFAULT_CONFIG_FILE

openssl 的默认系统配置文件

ESCAPE_MAP

逃逸的 char 映射

QUOTE_REGEXP_DQ

用反斜杠转义并将 dq 翻倍

QUOTE_REGEXP_SQ

用反斜杠转义

公共类方法

new(filename = nil) 显示源

创建一个 OpenSSL 配置类的实例。

这可以用在像 OpenSSL :: X509 :: ExtensionFactory.config =的上下文中

如果提供了可选filename参数,那么它将通过 parse_config 读入并解析。

这可能会根据访问或文件的可用性提高 IO 异常。根据所配置的数据的有效性,可能会产生 ConfigError 异常。

# File ext/openssl/lib/openssl/config.rb, line 245
def initialize(filename = nil)
  @data = {}
  if filename
    File.open(filename.to_s) do |file|
      Config.parse_config(file).each do |section, hash|
        self[section] = hash
      end
    end
  end
end

parse(string) 显示源

将给定解析string为包含 openssl 配置的 blob。

如果 IO 的源是一个文件,那么考虑使用 parse_config。

# File ext/openssl/lib/openssl/config.rb, line 35
def parse(string)
  c = new()
  parse_config(StringIO.new(string)).each do |section, hash|
    c[section] = hash
  end
  c
end

parse_config(io) 显示源

解析读取的配置数据io,另请参阅解析。

引发无效配置数据的 ConfigError。

# File ext/openssl/lib/openssl/config.rb, line 51
def parse_config(io)
  begin
    parse_config_lines(io)
  rescue ConfigError => e
    e.message.replace("error in line #{io.lineno}: " + e.message)
    raise
  end
end

私有类方法

clear_comments(line) 显示源

# File ext/openssl/lib/openssl/config.rb, line 176
def clear_comments(line)
  # FCOMMENT
  if m = line.match(/\A([\t\n\f ]*);.*\z/)
    return m[1]
  end
  # COMMENT
  scanned = []
  while m = line.match(/[#'"\]/)
    scanned << m.pre_match
    c = m[0]
    line = m.post_match
    case c
    when '#'
      line = nil
      break
    when "'", '"'
      regexp = (c == "'") ? QUOTE_REGEXP_SQ : QUOTE_REGEXP_DQ
      scanned << c
      if m = line.match(regexp)
        scanned << m[0]
        line = m.post_match
      else
        scanned << line
        line = nil
        break
      end
    when "\\"
      scanned << c
      scanned << line.slice!(0, 1)
    else
      raise 'must not reaced'
    end
  end
  scanned << line
  scanned.join
end

extract_reference(value) 显示源

# File ext/openssl/lib/openssl/config.rb, line 161
def extract_reference(value)
  rest = ''
  if m = value.match(/\(([^)]*)\)|\{([^}]*)\}/)
    value = m[1] || m[2]
    rest = m.post_match
  elsif [?(, ?{].include?(value[0])
    raise ConfigError, "no close brace"
  end
  if m = value.match(/[a-zA-Z0-9_]*(?:::[a-zA-Z0-9_]*)?/)
    return m[0], m.post_match + rest
  else
    raise
  end
end

get_definition(io) 显示源

# File ext/openssl/lib/openssl/config.rb, line 213
def get_definition(io)
  if line = get_line(io)
    while /[^\]\\z/ =~ line
      if extra = get_line(io)
        line += extra
      else
        break
      end
    end
    return line.strip
  end
end

get_line(io) 显示源

# File ext/openssl/lib/openssl/config.rb, line 226
def get_line(io)
  if line = io.gets
    line.gsub(/[\r\n]*/, '')
  end
end

parse_config_lines(io)显示源文件

# File ext/openssl/lib/openssl/config.rb, line 75
def parse_config_lines(io)
  section = 'default'
  data = {section => {}}
  while definition = get_definition(io)
    definition = clear_comments(definition)
    next if definition.empty?
    if definition[0] == ?[
      if /\[([^\]]*)\]/ =~ definition
        section = $1.strip
        data[section] ||= {}
      else
        raise ConfigError, "missing close square bracket"
      end
    else
      if /\A([^:\s]*)(?:::([^:\s]*))?\s*=(.*)\z/ =~ definition
        if $2
          section = $1
          key = $2
        else
          key = $1
        end
        value = unescape_value(data, section, $3)
        (data[section] ||= {})[key] = value.strip
      else
        raise ConfigError, "missing equal sign"
      end
    end
  end
  data
end

unescape_value(data, section, value) 显示源

# File ext/openssl/lib/openssl/config.rb, line 118
def unescape_value(data, section, value)
  scanned = []
  while m = value.match(/['"\$]/)
    scanned << m.pre_match
    c = m[0]
    value = m.post_match
    case c
    when "'"
      if m = value.match(QUOTE_REGEXP_SQ)
        scanned << m[1].gsub(/\(.)/, '\1')
        value = m.post_match
      else
        break
      end
    when '"'
      if m = value.match(QUOTE_REGEXP_DQ)
        scanned << m[1].gsub(/""/, '').gsub(/\(.)/, '\1')
        value = m.post_match
      else
        break
      end
    when "\\"
      c = value.slice!(0, 1)
      scanned << (ESCAPE_MAP[c] || c)
    when "$"
      ref, value = extract_reference(value)
      refsec = section
      if ref.index('::')
        refsec, ref = ref.split('::', 2)
      end
      if v = get_key_string(data, refsec, ref)
        scanned << v
      else
        raise ConfigError, "variable has no value"
      end
    else
      raise 'must not reaced'
    end
  end
  scanned << value
  scanned.join
end

公共实例方法

显示源

从当前配置获取特定section信息

鉴于正在加载以下配置文件:

config = OpenSSL::Config.load('foo.cnf')
  #=> #<OpenSSL::Config sections=["default"]>
puts config.to_s
  #=> [ default ]
  #   foo=bar

你可以像这样获得特定部分的哈希值:

config['default']
  #=> {"foo"=>"bar"}
# File ext/openssl/lib/openssl/config.rb, line 339
def [](section)
  @data[section] || {}
end

[]=(section, pairs) 显示源

用哈希pairs设置一个特定的section名字

鉴于正在创建以下配置:

config = OpenSSL::Config.new
  #=> #<OpenSSL::Config sections=[]>
config['default'] = {"foo"=>"bar","baz"=>"buz"}
  #=> {"foo"=>"bar", "baz"=>"buz"}
puts config.to_s
  #=> [ default ]
  #   foo=bar
  #   baz=buz

重要的是要注意,这将实质上将任何密钥pairs与现有密钥合并section。例如:

config['default']
  #=> {"foo"=>"bar", "baz"=>"buz"}
config['default'] = {"foo" => "changed"}
  #=> {"foo"=>"changed"}
config['default']
  #=> {"foo"=>"changed", "baz"=>"buz"}
# File ext/openssl/lib/openssl/config.rb, line 376
def []=(section, pairs)
  check_modify
  @data[section] ||= {}
  pairs.each do |key, value|
    self.add_value(section, key, value)
  end
end

add_value(section, key, value) 显示源

根据具体section情况设置key给定的value目标。

鉴于正在加载以下配置文件:

config = OpenSSL::Config.load('foo.cnf')
  #=> #<OpenSSL::Config sections=["default"]>
puts config.to_s
  #=> [ default ]
  #   foo=bar

您可以foo将该default部分下的值设置为新值:

config.add_value('default', 'foo', 'buzz')
  #=> "buzz"
puts config.to_s
  #=> [ default ]
  #   foo=buzz
# File ext/openssl/lib/openssl/config.rb, line 318
def add_value(section, key, value)
  check_modify
  (@data[section] ||= {})[key] = value
end

each() { |section, key, value| ... } 显示源

For a block.

接收当前配置的部分及其配对。

config.each do |section, key, value|
  # ...
end
# File ext/openssl/lib/openssl/config.rb, line 437
def each
  @data.each do |section, hash|
    hash.each do |key, value|
      yield [section, key, value]
    end
  end
end

get_value(节,键)显示源

获取key给定的值section

鉴于正在加载以下配置文件:

config = OpenSSL::Config.load('foo.cnf')
  #=> #<OpenSSL::Config sections=["default"]>
puts config.to_s
  #=> [ default ]
  #   foo=bar

你可以从配置中获得一个特定的值,如果你知道sectionkey像这样:

config.get_value('default','foo')
  #=> "bar"
# File ext/openssl/lib/openssl/config.rb, line 273
def get_value(section, key)
  if section.nil?
    raise TypeError.new('nil not allowed')
  end
  section = 'default' if section.empty?
  get_key_string(section, key)
end

inspect() 显示源

此配置对象的字符串表示形式,包括类名称及其部分。

# File ext/openssl/lib/openssl/config.rb, line 448
def inspect
  "#<#{self.class.name} sections=#{sections.inspect}>"
end

sections() 显示源

获取当前配置中所有部分的名称

# File ext/openssl/lib/openssl/config.rb, line 386
def sections
  @data.keys
end

to_s() 显示源

获取当前配置的可分析形式

鉴于正在创建以下配置:

config = OpenSSL::Config.new
  #=> #<OpenSSL::Config sections=[]>
config['default'] = {"foo"=>"bar","baz"=>"buz"}
  #=> {"foo"=>"bar", "baz"=>"buz"}
puts config.to_s
  #=> [ default ]
  #   foo=bar
  #   baz=buz

您可以使用 to_s 解析获取序列化配置,然后再解析它:

serialized_config = config.to_s
# much later...
new_config = OpenSSL::Config.parse(serialized_config)
  #=> #<OpenSSL::Config sections=["default"]>
puts new_config
  #=> [ default ]
      foo=bar
      baz=buz
# File ext/openssl/lib/openssl/config.rb, line 416
def to_s
  ary = []
  @data.keys.sort.each do |section|
    ary << "[ #{section} ]\n"
    @data[section].keys.each do |key|
      ary << "#{key}=#{@data[section][key]}\n"
    end
    ary << "\n"
  end
  ary.join
end

私有实例方法

check_modify() 显示源

# File ext/openssl/lib/openssl/config.rb, line 464
def check_modify
  raise TypeError.new("Insecure: can't modify OpenSSL config") if frozen?
end

get_key_string(section, key) 显示源

# File ext/openssl/lib/openssl/config.rb, line 468
def get_key_string(section, key)
  Config.get_key_string(@data, section, key)
end

initialize_copy(other) 显示源

# File ext/openssl/lib/openssl/config.rb, line 460
def initialize_copy(other)
  @data = other.data.dup
end

OpenSSL相关

1.OpenSSL::ASN1
2.OpenSSL::ASN1::ASN1Data
3.OpenSSL::ASN1::ASN1Error
4.OpenSSL::ASN1::Constructive
5.OpenSSL::ASN1::ObjectId
6.OpenSSL::ASN1::Primitive
7.OpenSSL::BN
8.OpenSSL::BNError
9.OpenSSL::Buffering
10.OpenSSL::Cipher
11.OpenSSL::Cipher::Cipher
12.OpenSSL::ConfigError
13.OpenSSL::Digest
14.OpenSSL::Digest::DigestError
15.OpenSSL::Engine
16.OpenSSL::Engine::EngineError
17.OpenSSL::ExtConfig
18.OpenSSL::HMAC
19.OpenSSL::HMACError
20.OpenSSL::Netscape
21.OpenSSL::Netscape::SPKI
22.OpenSSL::Netscape::SPKIError
23.OpenSSL::OCSP
24.OpenSSL::OCSP::BasicResponse
25.OpenSSL::OCSP::CertificateId
26.OpenSSL::OCSP::OCSPError
27.OpenSSL::OCSP::Request
28.OpenSSL::OCSP::Response
29.OpenSSL::OCSP::SingleResponse
30.OpenSSL::OpenSSLError
31.OpenSSL::PKCS12
32.OpenSSL::PKCS5
33.OpenSSL::PKCS5::PKCS5Error
34.OpenSSL::PKCS7
35.OpenSSL::PKCS7::RecipientInfo
36.OpenSSL::PKCS7::SignerInfo
37.OpenSSL::PKey
38.OpenSSL::PKey::DH
39.OpenSSL::PKey::DHError
40.OpenSSL::PKey::DSA
41.OpenSSL::PKey::DSAError
42.OpenSSL::PKey::EC
43.OpenSSL::PKey::EC::Group
44.OpenSSL::PKey::EC::Point
45.OpenSSL::PKey::PKey
46.OpenSSL::PKey::PKeyError
47.OpenSSL::PKey::RSA
48.OpenSSL::PKey::RSAError
49.OpenSSL::Random
50.OpenSSL::SSL
51.OpenSSL::SSL::Session
52.OpenSSL::SSL::SocketForwarder
53.OpenSSL::SSL::SSLContext
54.OpenSSL::SSL::SSLError
55.OpenSSL::SSL::SSLServer
56.OpenSSL::SSL::SSLSocket
57.OpenSSL::X509::Attribute
58.OpenSSL::X509::Certificate
59.OpenSSL::X509::CRL
60.OpenSSL::X509::Extension
61.OpenSSL::X509::ExtensionFactory
62.OpenSSL::X509::Name
63.OpenSSL::X509::Name::RFC2253DN
64.OpenSSL::X509::Request
65.OpenSSL::X509::Revoked
66.OpenSSL::X509::Store
67.OpenSSL::X509::StoreContext
Ruby 2.4

Ruby 是一种面向对象、命令式、函数式、动态的通用编程语言,是世界上最优美而巧妙的语言。

主页 https://www.ruby-lang.org/
源码 https://github.com/ruby/ruby
版本 2.4
发布版本 2.4.1

Ruby 2.4目录

1.缩略 | Abbrev
2.ARGF
3.数组 | Array
4.Base64
5.基本对象 | BasicObject
6.基准测试 | Benchmark
7.BigDecimal
8.绑定 | Binding
9.CGI
10.类 | Class
11.比较 | Comparable
12.负责 | Complex
13.计算续体 | Continuation
14.覆盖 | Coverage
15.CSV
16.日期 | Date
17.日期时间 | DateTime
18.DBM
19.代理 | Delegator
20.摘要 | Digest
21.Dir
22.DRb
23.编码 | Encoding
24.枚举 | Enumerable
25.枚举 | Enumerator
26.ENV
27.ERB
28.错误 | Errors
29.Etc
30.期望值 | Exception
31.错误类 | FalseClass
32.Fiber
33.Fiddle
34.文件 | File
35.文件实用程序 | FileUtils
36.查找 | Find
37.浮点 | Float
38.Forwardable
39.GC
40.GDBM
41.GetoptLong
42.Hash
43.Integer
44.IO
45.IPAddr
46.JSON
47.Kernel
48.语言 | 3Language
49.记录 | Logger
50.编排 | Marshal
51.MatchData
52.数学 | Math
53.矩阵 | Matrix
54.方法 | Method
55.模型 | Module
56.监控 | Monitor
57. 互斥 | Mutex
58.Net
59.Net::FTP
60.Net::HTTP
61.Net::IMAP
62.Net::SMTP
63.NilClass
64.数字 | Numeric
65.对象 | Object
66.ObjectSpace
67.Observable
68.Open3
69.OpenSSL
70.OpenStruct
71.OpenURI
72.OptionParser
73.路径名 | Pathname
74.完整输出 | PrettyPrint
75.Prime
76.Proc
77.过程 | Process
78.PStore
79.PTY
80.队列 | Queue
81.随机 | Random
82.范围 | Range
83.合理的 | Rational
84.Readline
85.Regexp
86.Resolv
87.Ripper
88.RubyVM
89.Scanf
90.SDBM
91.SecureRandom
92.Set
93.Shell
94.信号 | Signal
95.Singleton
96.套接字 | Socket
97.字符串 | String
98.StringIO
99.StringScanner
100.结构 | Struct