Puppet Function: tor::onion_address

Defined in:
lib/puppet/functions/tor/onion_address.rb
Function type:
Ruby 4.x API

Overview

tor::onion_address(Any *$args)Any

Parameters:

  • *args (Any)

Returns:

  • (Any)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/puppet/functions/tor/onion_address.rb', line 14

Puppet::Functions.create_function(:'tor::onion_address') do
  dispatch :default_impl do
    repeated_param 'Any', :args
  end


  def default_impl(*args)
    
    key = args.shift
    raise(Puppet::ParseError, "tor::onion_address(): requires 1 argument") unless key && args.empty?
    private_key = key.is_a?(OpenSSL::PKey::RSA) ? key : OpenSSL::PKey::RSA.new(key)

    # the onion address are a base32 encoded string of the first half of the
    # sha1 over the der format of the public key
    # https://trac.torproject.org/projects/tor/wiki/doc/HiddenServiceNames#Howare.onionnamescreated
    # We can skip the first 22 bits of the der format as they are ignored by tor
    # https://timtaubert.de/blog/2014/11/using-the-webcrypto-api-to-generate-onion-names-for-tor-hidden-services/
    # https://gitweb.torproject.org/torspec.git/tree/rend-spec.txt#n525
    public_key_der = private_key.public_key.to_der
    Base32.encode(Digest::SHA1.digest(public_key_der[22..-1]))[0..15].downcase
  
  end
end