forked from wolfgangw/digital_cinema_tools_distribution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublic_key_thumbprint.rb
executable file
·49 lines (44 loc) · 1.65 KB
/
public_key_thumbprint.rb
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env ruby
#
# Calculate public key thumbprint of X509 certificates
# 2012 Wolfgang Woehl
#
# See https://bugs.ruby-lang.org/issues/4421 for a related ticket and discussion
#
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
AppName = File.basename __FILE__
require 'openssl'
require 'base64'
if ARGV.size < 1
puts "Usage: #{ AppName } <cert> [<more certs>]"
exit 1
end
ARGV.each do |arg|
begin
cert = OpenSSL::X509::Certificate.new( open arg )
rescue Exception => e
puts "#{ arg }: #{ e.inspect }"
next
end
pkey_der = OpenSSL::ASN1::Sequence( [ OpenSSL::ASN1::Integer( cert.public_key.n ), OpenSSL::ASN1::Integer( cert.public_key.e ) ] ).to_der
public_key_thumbprint = OpenSSL::Digest.new( 'sha1', pkey_der ).digest
public_key_thumbprint_b64 = Base64.encode64( public_key_thumbprint ).chomp
public_key_thumbprint_b16 = public_key_thumbprint.unpack( 'H2' * 20 ).join
puts "Subject: #{ cert.subject.to_s }"
puts "Public key thumbprint b64: #{ public_key_thumbprint_b64 }"
puts "Public key thumbprint b16: #{ public_key_thumbprint_b16 }"
end
exit 0