Class: AquaticPrime
- Inherits:
-
Object
- Object
- AquaticPrime
- Defined in:
- lib/aquaticprime.rb
Overview
AquaticPrime instances are associated with a given public/private key pair, and generate signed license plists for input Hash instances.
Instance Method Summary collapse
-
#initialize(pubKey, privKey) ⇒ AquaticPrime
constructor
Returns a new AquaticPrime generator with the given public and private keys.
-
#license_data(license_info) ⇒ Object
Returns a String containing a signed license plist based on the given license_info Hash.
-
#signature(information) ⇒ Object
Calculates the cryptographic signature for a given Hash, returned as a String.
Constructor Details
#initialize(pubKey, privKey) ⇒ AquaticPrime
Returns a new AquaticPrime generator with the given public and private keys. Keys should be provided as hex strings.
42 43 44 45 |
# File 'lib/aquaticprime.rb', line 42 def initialize(pubKey, privKey) @pubKey = pubKey @privKey = privKey end |
Instance Method Details
#license_data(license_info) ⇒ Object
Returns a String containing a signed license plist based on the given license_info Hash. The result is suitable content for a license file via a download, email attachment, or any other delivery mechanism to the end user.
71 72 73 74 75 76 77 78 79 |
# File 'lib/aquaticprime.rb', line 71 def license_data(license_info) signed_license_info = license_info.dup # Sign the license info. # If a value in the plist is a StringIO object, it handily ends up as a base64-encoded <data> key signed_license_info['Signature'] = StringIO.new(signature(license_info)) signed_license_info.to_plist end |
#signature(information) ⇒ Object
Calculates the cryptographic signature for a given Hash, returned as a String. This is generally only of internal interest, but on rare instances clients may wish to calculate this value.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/aquaticprime.rb', line 51 def signature(information) total = information.sort{|a,b| a[0].downcase <=> b[0].downcase || a[0] <=> b[0]}.map{|key,value| value}.join('') hash = Digest::SHA1.hexdigest(total) hash = '0001' + ('ff' * 105) + '00' + hash sig = Math.powmod(hash.hex, @privKey.hex, @pubKey.hex) # Convert from a big number to a binary string. sig = sig.to_s(16) sig = ('0' * (256 - sig.length)) + sig sig = sig.unpack('a2' * (sig.length/2)).map { |x| x.hex }.pack('c' * (sig.length/2)) sig end |