Skip to content

Instantly share code, notes, and snippets.

@cameroncox
Created March 16, 2012 19:19
Show Gist options
  • Save cameroncox/2051969 to your computer and use it in GitHub Desktop.
Save cameroncox/2051969 to your computer and use it in GitHub Desktop.
module Caracal
class DecodeProductId
def initialize(digital_product_id)
@digital_product_id = digital_product_id
end
def self.run(digital_product_id)
new(digital_product_id).decode
end
def decode
extracted_encoded_product_key = extract_encoded_product_key
key, i = "", 24
while i >= 0
index = 0
j = 14
while j >= 0
index = (index << 8) | extracted_encoded_product_key[j].to_i
extracted_encoded_product_key[j] = (index / 24)
index = (index % 24)
j -= 1
end
key = valid_chars_in_product_key[index].chr + key
key = ("-" + key) if ((i % 5 == 0) and i > 0)
i -= 1
end
key
end
private
def valid_chars_in_product_key
"BCDFGHJKMPQRTVWXY2346789".scan(/./).map(&:ord)
end
def extract_encoded_product_key
digital_product_id_as_array.slice(52, 15)
end
def digital_product_id_as_array
@digital_product_id.unpack('H*').first.gsub(/(..)/, '\1 ').split(' ').map { |x| x.to_i(16) }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment