Skip to content

Commit 8a20790

Browse files
committed
add high-level wrapper and document in README.md
1 parent 177301c commit 8a20790

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

README.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,29 @@ Or install it yourself as:
2020

2121
## Usage
2222

23-
TODO: Write usage instructions here
23+
To encrypt, you can either pass in a file or binary data:
24+
```ruby
25+
require "ooxml_crypt"
26+
27+
# pass in binary data, get binary data
28+
encrypted_data = OoxmlCrypt.encrypt(bindata, password)
29+
30+
# or use file names
31+
OoxmlCrypt.encrypt_file(filename, password, encrypted_filename)
32+
```
33+
34+
The same approach holds true for decryption:
35+
```ruby
36+
require "ooxml_crypt"
37+
38+
# pass in binary data, get binary data
39+
decrypted_data = OoxmlCrypt.decrypt(bindata, password)
40+
41+
# or use file names
42+
OoxmlCrypt.decrypt_file(filename, password, decrypted_filename)
43+
```
2444

45+
An exception `OoxmlCrypt::Error` will be raised in case of errors.
2546
## Development
2647

2748
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

lib/ooxml_crypt.rb

+68
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,73 @@
33
require_relative "ooxml_crypt/version"
44
require_relative "ooxml_crypt/ooxml_crypt"
55

6+
require "tempfile"
7+
68
module OoxmlCrypt
9+
class Error < StandardError; end
10+
class FileNotFound < Error; end
11+
class EmptyPassword < Error; end
12+
13+
ERRORS = { # should be kept in sync with msoc.h
14+
1 => "not supported format",
15+
2 => "already encrypted",
16+
3 => "already decrypted",
17+
4 => "bad password",
18+
5 => "bad parameter",
19+
6 => "small max size",
20+
7 => "no memory",
21+
8 => "internal exception",
22+
9 => "too large file",
23+
10 => "inFile is empty",
24+
11 => "outFile is empty",
25+
12 => "password is empty",
26+
}
27+
28+
def self.encrypt_file(input, password, output)
29+
raise EmptyPassword if password.nil? || password.empty?
30+
raise FileNotFound, input unless File.exist?(input)
31+
32+
result = OoxmlCryptNative.encrypt_file(input, password, output)
33+
raise Error, ERRORS[-result] if result != 0
34+
end
35+
36+
def self.decrypt_file(input, password, output)
37+
raise EmptyPassword if password.nil? || password.empty?
38+
raise FileNotFound, input unless File.exist?(input)
39+
40+
result = OoxmlCryptNative.decrypt_file(input, password, output)
41+
raise Error, ERRORS[-result] if result != 0
42+
end
43+
44+
def self.encrypt(data, password)
45+
with_temp_files(data) do |input, output|
46+
encrypt_file(input, password, output)
47+
end
48+
end
49+
50+
def self.decrypt(data, password)
51+
with_temp_files(data) do |input, output|
52+
decrypt_file(input, password, output)
53+
end
54+
end
55+
56+
private
57+
58+
def self.with_temp_files(data)
59+
input = Tempfile.new("ooxml_crypt_input", binmode: true)
60+
input.write(data)
61+
input.close
62+
63+
output = Tempfile.new("ooxml_crypt_output", binmode: true)
64+
output.close
65+
66+
yield input.path, output.path
67+
68+
result = File.binread(output.path)
69+
70+
input.unlink
71+
output.unlink
72+
73+
result
74+
end
775
end

0 commit comments

Comments
 (0)