Module: Aspera::SealedHash
- Defined in:
- lib/aspera/sealed_hash.rb
Overview
Converts a Hash to a compact text, typed or pasted by a user, and back. Only Ruby standard libraries are used.
Used for the codes exchanged by the user between ascli and the browser, or Shares (option ui.web_auth=paste).
Steps: Hash -> JSON -> deflate -> AES-256-GCM -> Base64 URL-safe, without padding (characters: A-Z a-z 0-9 - _).
Text layout, once decoded from Base64: IV (12 bytes) + authentication tag (16 bytes) + encrypted data.
The key is SHA256(<base key> + <secret> + <slot>), where slot is the Unix time divided by the period (15 minutes by default), rounded down.
Base key and secret are optional.
The authentication tag detects an incomplete, mistyped or modified text, or a wrong key.
There is no prefix nor type in the text: the receiver checks the keys of the Hash.
The protection depends on the key: if the base key is a constant (e.g. in the source code) and there is no secret, the key is not secret, and the slot is easy to guess:
the text is then not readable at first sight and is not valid for long, but it is not confidential.
To make it confidential, give a secret known only by both sides (e.g. a random state).
The slot changes every period, and the clocks of both sides can differ: unseal also tries the previous slot and the next one.
So a text is valid from 1 to 2 periods after its creation (15 to 30 minutes by default), and is also accepted if the clock of the receiver is up to 1 period behind.
When unsealing, spaces and line breaks are ignored (a text split on several lines is valid), as well as quotes and punctuation around the text.
Letter case matters.
Unless reversed, the text never starts with -, so that it can be given as an argument on a command line (it would be read as an option):
with encryption, an IV that gives - is drawn again; without, the text starts with the header of deflate.
With encrypt: false, there is no encryption and no validity period: the text is only Hash -> JSON -> deflate -> Base64 URL-safe, readable by anyone, without detection of errors.
It is used for links that must stay valid and are decoded by other programs (e.g. JavaScript in a browser).
sealed? does not know this mode.
With reverse: true, the final text is reversed (it must be the same on both sides), in any mode.
Constant Summary collapse
- PERIOD =
Duration of a slot, in seconds
900
Class Method Summary collapse
-
.seal(data, secret: nil, base_key: nil, time: Time.now, period: PERIOD, encrypt: true, reverse: false) ⇒ String
Base64 URL-safe text, without padding.
-
.sealed?(text) ⇒ Boolean
trueif text looks like a sealed hash (characters and length), to be decoded withunseal. -
.unseal(text, secret: nil, base_key: nil, time: Time.now, period: PERIOD, encrypt: true, reverse: false) ⇒ Hash
The data.
Class Method Details
.seal(data, secret: nil, base_key: nil, time: Time.now, period: PERIOD, encrypt: true, reverse: false) ⇒ String
Returns Base64 URL-safe text, without padding.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/aspera/sealed_hash.rb', line 64 def seal(data, secret: nil, base_key: nil, time: Time.now, period: PERIOD, encrypt: true, reverse: false) Aspera.assert_type(data, Hash) deflated = Zlib::Deflate.deflate(JSON.generate(data), Zlib::BEST_COMPRESSION) raw = if encrypt cipher = OpenSSL::Cipher.new(CIPHER).encrypt cipher.key = derive_key(base_key, secret, time.to_i / period) iv = cipher.random_iv # The text starts with the IV: an IV that gives `-` as first character (`+` before URL-safe) is drawn again iv = cipher.random_iv while [iv].pack('m0').start_with?('+') body = cipher.update(deflated) + cipher.final iv + cipher.auth_tag(TAG_SIZE) + body else deflated end text = [raw].pack('m0').tr('+/', '-_').delete('=') reverse ? text.reverse : text end |
.sealed?(text) ⇒ Boolean
Returns true if text looks like a sealed hash (characters and length), to be decoded with unseal.
A URL or a query always has other characters (:, /, ?, =, &).
86 87 88 89 |
# File 'lib/aspera/sealed_hash.rb', line 86 def sealed?(text) clean = normalize(text) clean.length >= MIN_TEXT_LENGTH && clean.match?(TEXT_CHARS) end |
.unseal(text, secret: nil, base_key: nil, time: Time.now, period: PERIOD, encrypt: true, reverse: false) ⇒ Hash
Returns The data.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/aspera/sealed_hash.rb', line 100 def unseal(text, secret: nil, base_key: nil, time: Time.now, period: PERIOD, encrypt: true, reverse: false) clean = normalize(text) clean = clean.reverse if reverse raise ParameterError, 'Code is too long' if clean.length > MAX_TEXT_LENGTH raise ParameterError, 'Code has invalid characters: only letters, digits, - and _ are expected' unless clean.match?(TEXT_CHARS) raise ParameterError, 'Code is incomplete' if clean.length < (encrypt ? MIN_TEXT_LENGTH : 1) raw = begin "#{clean.tr('-_', '+/')}#{'=' * ((4 - (clean.length % 4)) % 4)}".unpack1('m0') rescue ArgumentError raise ParameterError, 'Code is incomplete or has typing errors' end if encrypt deflated = nil SLOT_OFFSETS.each do |offset| deflated = decrypt(raw, derive_key(base_key, secret, (time.to_i / period) + offset)) break unless deflated.nil? end raise ParameterError, 'Invalid code: incomplete, mistyped, expired, or the clock is wrong (check the time of both computers)' if deflated.nil? else deflated = raw end data = JSON.parse(Inflate.limited(deflated, max_size: MAX_INFLATED_SIZE, what: 'Code')) raise ParameterError, 'Invalid code: unexpected content' unless data.is_a?(Hash) data rescue JSON::ParserError, Zlib::Error raise ParameterError, 'Invalid code: unexpected content' end |