Skip to content

Instantly share code, notes, and snippets.

@qffdn
Created August 16, 2017 05:10
Show Gist options
  • Save qffdn/52468455cb8a552f9ffb88cd0c218a4b to your computer and use it in GitHub Desktop.
Save qffdn/52468455cb8a552f9ffb88cd0c218a4b to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'openssl'
RSA_PUBLIC_KEY_PEM = <<EOK
-----BEGIN PUBLIC KEY-----
MIGdMA0GCSqGSIb3DQEBAQUAA4GLADCBhwKBgQDYe6JFYvfF0UoM+xK5dAwZXGvc
fm1uySusDrKdWeHZrmeJDCuIw6vcr/59SjPcwb++UxolHO8Mkj8GvnmyMoVZrP7p
htXhXk0XZupWxOEGV/p02wl3w/t1greM1Huyx/myUrSpRj0V9q5u6SN9VMVIG/Pg
sJkgGQvPsx5b5QnDOwIBEQ==
-----END PUBLIC KEY-----
EOK
abort("Usage: #{$0} license file") if ARGV.length < 1
lines = nil
File.open(ARGV[0], 'rt') do |f|
# rebuilding array with \n down below because Windows users may have \r\n
lines = f.readlines.map {|l| l.strip}
end
abort('License file too short') if lines.length < 13
abort('Missing top license marker') if lines[0] != '----- BEGIN LICENSE -----'
abort('Missing bottom license marker') if lines[12] != '----- END LICENSE -----'
user_info = "#{lines[1]}\n#{lines[2]}\n#{lines[3]}"
sig = lines[4..11].join.strip
sig = sig.tr(' ', '').scan(/../).map {|x| x.hex.chr }.join
rsa = OpenSSL::PKey::RSA.new(RSA_PUBLIC_KEY_PEM)
is_valid = rsa.verify(OpenSSL::Digest::SHA1.new, sig, user_info)
puts("The license signature is #{is_valid ? '' : 'in'}valid.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment