Class: Axlsx::MsOffCrypto
- Inherits:
-
Object
- Object
- Axlsx::MsOffCrypto
- Defined in:
- lib/axlsx/util/ms_off_crypto.rb
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
2.3.4.7 ECMA-376 Document Encryption Key Generation (Standard Encryption).
-
#verifier ⇒ Object
readonly
Returns the value of attribute verifier.
Instance Method Summary collapse
- #decrypt(data) ⇒ Object
- #encrypt(data) ⇒ Object
-
#encrypted_verifier ⇒ Object
2.3.3.
-
#encrypted_verifier_hash ⇒ Object
2.3.3.
- #encryption_info ⇒ Object
- #encryption_verifier ⇒ Object
-
#initialize(password = "passowrd") ⇒ MsOffCrypto
constructor
A new instance of MsOffCrypto.
- #verify_password ⇒ Object
Constructor Details
#initialize(password = "passowrd") ⇒ MsOffCrypto
Returns a new instance of MsOffCrypto.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/axlsx/util/ms_off_crypto.rb', line 10 def initialize(password = "passowrd") @password = password @salt_size = 0x10 @key_size = 0x100 @verifier = rand(16**16).to_s #fixed salt for testing @salt = [0x90,0xAC,0x68,0x0E,0x76,0xF9,0x43,0x2B,0x8D,0x13,0xB7,0x1D,0xB7,0xC0,0xFC,0x0D].join # @salt =Digest::SHA1.digest(rand(16**16).to_s) end |
Instance Attribute Details
#key ⇒ Object (readonly)
2.3.4.7 ECMA-376 Document Encryption Key Generation (Standard Encryption)
57 58 59 |
# File 'lib/axlsx/util/ms_off_crypto.rb', line 57 def key @key end |
#verifier ⇒ Object (readonly)
Returns the value of attribute verifier.
7 8 9 |
# File 'lib/axlsx/util/ms_off_crypto.rb', line 7 def verifier @verifier end |
Instance Method Details
#decrypt(data) ⇒ Object
80 81 82 83 84 85 |
# File 'lib/axlsx/util/ms_off_crypto.rb', line 80 def decrypt(data) aes = OpenSSL::Cipher.new("AES-128-ECB") aes.decrypt aes.key = key aes.update(data) end |
#encrypt(data) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/axlsx/util/ms_off_crypto.rb', line 73 def encrypt(data) aes = OpenSSL::Cipher.new("AES-128-ECB") aes.encrypt aes.key = key aes.update(data) end |
#encrypted_verifier ⇒ Object
2.3.3
45 46 47 |
# File 'lib/axlsx/util/ms_off_crypto.rb', line 45 def encrypted_verifier @encrypted_verifier ||= encrypt(@verifier) end |
#encrypted_verifier_hash ⇒ Object
2.3.3
50 51 52 53 54 |
# File 'lib/axlsx/util/ms_off_crypto.rb', line 50 def encrypted_verifier_hash verifier_hash = Digest::SHA1.digest(@verifier) verifier_hash << Array.new(32 - verifier_hash.size, 0).join('') @encrypted_verifier_hash ||= encrypt(verifier_hash) end |
#encryption_info ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/axlsx/util/ms_off_crypto.rb', line 21 def encryption_info # v.major v.minor flags header length flags size # AES 128 bit header = [3, 0, 2, 0, 0x24, 0, 0, 0, 0xA4, 0, 0, 0, 0x24, 0, 0, 0, 0, 0, 0, 0, 0x0E, 0x66, 0, 0] header.concat [0x04, 0x80, 0, 0, 0x80, 0, 0, 0, 0x18, 0, 0, 0, 0xA0, 0xC7, 0xDC, 0x2, 0, 0, 0, 0] header.concat "Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)".bytes.to_a.pack('s*').bytes.to_a header.concat [0, 0] header.concat [0x10, 0, 0, 0] header.concat [0x90,0xAC,0x68,0x0E,0x76,0xF9,0x43,0x2B,0x8D,0x13,0xB7,0x1D,0xB7,0xC0,0xFC,0x0D] header.concat encrypted_verifier.bytes.to_a.pack('c*').bytes.to_a header.concat [20, 0,0,0] header.concat encrypted_verifier_hash.bytes.to_a.pack('c*').bytes.to_a header.flatten! header.pack('c*') end |
#encryption_verifier ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/axlsx/util/ms_off_crypto.rb', line 36 def encryption_verifier {:salt_size => @salt_size, :salt => @salt, :encrypted_verifier => encrypted_verifier, :varifier_hash_size => 0x14, :encrypted_verifier_hash => encrypted_verifier_hash} end |
#verify_password ⇒ Object
69 70 71 |
# File 'lib/axlsx/util/ms_off_crypto.rb', line 69 def verify_password puts decrypt(@encrypted_verifier) end |