You need to sign in or sign up before continuing.

static_encrypt 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 634432305d11c47b1ffe73ae7e725f893924f71238981f5365c602927bf3d5b0
4
- data.tar.gz: 958774bdc70b2eecfde3bf11f4559472aab1ca08d290d0e71b5f03c511051883
3
+ metadata.gz: ffbb65d82985fbebcd83f1f84df9bcceb677b71d2b4f730ddd7c356fd862c866
4
+ data.tar.gz: c19c67e6a65e2421eb09c79c3c0845b95b55650383844bb8efb481b420dbe0bf
5
5
  SHA512:
6
- metadata.gz: 2e58edcff5e9d2c1b8c7dd649579541b023443ea1b284f36259317a6d94304b05b66ddaf41a753428571e7dfd1310b3ee85cc9fc1a66d0c8ecd72a203053f296
7
- data.tar.gz: da22f8511ee70bbad643845df34b8756e088211c15b389fb58c6bb86c5a0bb4e123f439c0224666e34782fac6730b23b7e741236c569098834dc4a3b7f98e6b3
6
+ metadata.gz: e5a517e3cbd72e178ee6ebbc3426307ef5b243188a3c01ea92c353fe8933fae1aeb55092b32d757ade8b4be6dd66fcb4818a93dc96a1801969e22b29b6253ef6
7
+ data.tar.gz: 6858f16a460824730f3cbd6bec720458ce951857d43c4023bbbc1901a874844012331580588c77058b7b49c1244ceb29f1330b51a823bde86a30746ca9e9467d
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../lib/static_encrypt/cli"
5
+
6
+ StaticEncrypt::CLI.start
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "optparse"
4
+ require "io/console"
5
+ require_relative "../static_encrypt"
6
+
7
+ module StaticEncrypt
8
+ # The static-encrypt command: encrypts a file into a password-protected HTML page.
9
+ # Run it with --help to see the options.
10
+ class CLI
11
+ PASSWORD_ENV = "STATIC_ENCRYPT_PASSWORD"
12
+
13
+ def self.start(argv = ARGV) = exit(new.run(argv))
14
+
15
+ def initialize(env: ENV, stdin: $stdin, stdout: $stdout, stderr: $stderr)
16
+ @env = env
17
+ @stdin = stdin
18
+ @stdout = stdout
19
+ @stderr = stderr
20
+ end
21
+
22
+ def run(argv)
23
+ options = parse(argv.dup)
24
+ return 0 if options[:exit]
25
+
26
+ input = options.delete(:input)
27
+ raise Error, "no such file: #{input}" unless File.file?(input)
28
+
29
+ # Read and check the template before asking for a password.
30
+ options[:template] = Page.validate_template!(File.read(options[:template])) if options[:template]
31
+
32
+ output = options.delete(:output) || default_output(input)
33
+ File.write(output, StaticEncrypt.encrypt_file(input, password(options.delete(:password)), **options))
34
+ @stdout.puts "Encrypted #{input} -> #{output}"
35
+ 0
36
+ rescue Error, OptionParser::ParseError, SystemCallError => e
37
+ @stderr.puts "static-encrypt: #{e.message}"
38
+ 1
39
+ end
40
+
41
+ private
42
+
43
+ def parse(argv)
44
+ options = {}
45
+ parser = option_parser
46
+ parser.parse!(argv, into: options)
47
+
48
+ return { exit: true } if options.keys.intersect?(%i[print-template version help])
49
+
50
+ options[:input] = argv.shift or raise Error, "missing FILE\n#{parser}"
51
+ options
52
+ end
53
+
54
+ def option_parser
55
+ OptionParser.new do |o|
56
+ o.banner = "Usage: static-encrypt FILE [options]"
57
+ o.version = VERSION
58
+ o.on("-p", "--password PASSWORD", "Password (else $#{PASSWORD_ENV}, else prompt)")
59
+ o.on("-o", "--output PATH", "Output file (default: home.html -> home.encrypted.html)")
60
+ o.on("-t", "--title TITLE", "Title shown on the password page")
61
+ o.on("-i", "--instructions TEXT", "Text shown under the title")
62
+ o.on("--template PATH", "Custom HTML template (start from --print-template)")
63
+ o.on("--print-template", "Print the default template and exit") do
64
+ @stdout.print Page.default_template
65
+ end
66
+ o.on("--iterations N", Integer, "PBKDF2 iterations (default: #{Cipher::DEFAULT_ITERATIONS})") do |n|
67
+ n.positive? ? n : raise(Error, "--iterations must be at least 1")
68
+ end
69
+ o.on("-v", "--version", "Print the version") { @stdout.puts VERSION }
70
+ o.on("-h", "--help", "Show this help") { @stdout.puts o }
71
+ end
72
+ end
73
+
74
+ def default_output(input)
75
+ ext = File.extname(input)
76
+ base = %w[.html .htm].include?(ext.downcase) ? input.delete_suffix(ext) : input
77
+ "#{base}.encrypted.html"
78
+ end
79
+
80
+ def password(given)
81
+ password = given || @env[PASSWORD_ENV] || prompt_password
82
+ raise Error, "password must not be empty" if password.empty?
83
+
84
+ password
85
+ end
86
+
87
+ def prompt_password
88
+ raise Error, "no password given and stdin is not a terminal" unless @stdin.tty?
89
+
90
+ password = @stdin.getpass("Password: ")
91
+ confirmation = @stdin.getpass("Confirmation: ")
92
+
93
+ raise Error, "passwords do not match" unless confirmation == password
94
+
95
+ password
96
+ end
97
+ end
98
+ end
@@ -4,6 +4,8 @@ require "cgi/escape"
4
4
  require "json"
5
5
 
6
6
  module StaticEncrypt
7
+ # Builds the self-contained HTML page that holds an encrypted file and decrypts
8
+ # it in the browser once the visitor enters the password.
7
9
  module Page
8
10
  TEMPLATE_PATH = File.expand_path("template.html", __dir__)
9
11
  DECRYPTOR_PATH = File.expand_path("decryptor.js", __dir__)
@@ -47,7 +49,16 @@ module StaticEncrypt
47
49
 
48
50
  def script(payload)
49
51
  json = JSON.generate(payload).gsub("<", "\\u003c")
50
- "<script>\n(() => {\n\"use strict\";\nconst payload = #{json};\n#{decryptor}})();\n</script>"
52
+
53
+ <<~SCRIPT
54
+ <script>
55
+ (() => {
56
+ "use strict"
57
+ const payload = #{json};
58
+ #{decryptor}
59
+ })();
60
+ </script>
61
+ SCRIPT
51
62
  end
52
63
 
53
64
  def validate_template!(template)
@@ -65,6 +76,7 @@ module StaticEncrypt
65
76
  end
66
77
 
67
78
  def payload(html)
79
+ html = String.new(html, encoding: Encoding::UTF_8).scrub
68
80
  json = html[/const payload = (\{.*?\});$/, 1] or raise Error, "no encrypted payload found"
69
81
  JSON.parse(json, symbolize_names: true)
70
82
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StaticEncrypt
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -4,7 +4,22 @@ require_relative "static_encrypt/version"
4
4
  require_relative "static_encrypt/cipher"
5
5
  require_relative "static_encrypt/page"
6
6
 
7
+ # Password-protect files as self-contained static HTML pages.
8
+ #
9
+ # The file is encrypted with AES-256-GCM using a key derived from the password
10
+ # with PBKDF2-HMAC-SHA256. The resulting page contains only the ciphertext plus
11
+ # a small WebCrypto-based decryptor, so it can be hosted anywhere. HTML files
12
+ # are rendered in place once decrypted; any other file is offered as a download.
13
+ #
14
+ # html = StaticEncrypt.encrypt_file("page.html", "hunter2", title: "My Page")
15
+ # File.write("protected.html", html)
7
16
  module StaticEncrypt
8
17
  class Error < StandardError; end
9
18
  class DecryptionError < Error; end
19
+
20
+ module_function
21
+
22
+ def encrypt_file(path, password, **)
23
+ Page.build(File.binread(path), password, filename: File.basename(path), **)
24
+ end
10
25
  end
metadata CHANGED
@@ -1,11 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: static_encrypt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniël de Vries
8
- bindir: bin
8
+ bindir: exe
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies: []
@@ -14,14 +14,17 @@ description: |
14
14
  single HTML page that decrypts in the browser with WebCrypto. Host it anywhere.
15
15
  email:
16
16
  - contact@danieldevries.eu
17
- executables: []
17
+ executables:
18
+ - static-encrypt
18
19
  extensions: []
19
20
  extra_rdoc_files: []
20
21
  files:
21
22
  - LICENSE.txt
22
23
  - README.md
24
+ - exe/static-encrypt
23
25
  - lib/static_encrypt.rb
24
26
  - lib/static_encrypt/cipher.rb
27
+ - lib/static_encrypt/cli.rb
25
28
  - lib/static_encrypt/decryptor.js
26
29
  - lib/static_encrypt/page.rb
27
30
  - lib/static_encrypt/template.html
@@ -39,7 +42,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
39
42
  requirements:
40
43
  - - ">="
41
44
  - !ruby/object:Gem::Version
42
- version: 3.2.0
45
+ version: 3.3.0
43
46
  required_rubygems_version: !ruby/object:Gem::Requirement
44
47
  requirements:
45
48
  - - ">="