enigma-rb 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f691900c9f844780a09317e57903bd139526e8150b576c7c698f35b13eec0e80
4
+ data.tar.gz: 598aed28768abee375e5684102b7891c73a6c3540188f70490503259525b1f7f
5
+ SHA512:
6
+ metadata.gz: b033557d0da1ecfffcbf0c9565b24b9b0ff384b78e339ed77fa4ccc92bd3586c1261cf6afc47ac9ae77bf59259661d69f82039db51ff2389e6c6ba9631728c53
7
+ data.tar.gz: e9e96d6d20fc1cefde3d27fe4b41204906073f4bd6e3b06d7c0ea60386828a8dd4b78eddff3c9625cae4c96a7b0fc0c49ebd9b4802fb30c1adb7a774cac34132
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.1
3
+
4
+ Style/StringLiterals:
5
+ EnforcedStyle: double_quotes
6
+
7
+ Style/StringLiteralsInInterpolation:
8
+ EnforcedStyle: double_quotes
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-11-11
4
+
5
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Yavor Dashev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,108 @@
1
+ # Enigma
2
+
3
+
4
+ Enigma: Firebase-Compatible Password Verifier for Ruby
5
+ Enigma is a lightweight Ruby gem designed to verify passwords hashed using Firebase's custom scrypt-based algorithm, making it ideal for seamless integrations and migrations involving Firebase authentication systems. It provides a secure, efficient way to compare a user-provided password against a stored hash without exposing sensitive details, ensuring constant-time comparisons to mitigate timing attacks.
6
+ Key Features
7
+
8
+ Firebase Compatibility: Implements the exact password verification logic used by Firebase Authentication, including scrypt hashing combined with AES-256-CTR encryption for signing.
9
+ Configurable Parameters: Easily customize scrypt parameters (rounds, memory cost), signer keys, and salt separators via a simple configuration block or per-instance overrides.
10
+ Secure Practices: Utilizes OpenSSL's fixed-length secure comparison (when available) to prevent side-channel vulnerabilities.
11
+ Logging Support: Integrates with any Ruby logger (defaults to STDERR) for error tracking, with Rails compatibility out of the box.
12
+ Minimal Dependencies: Relies only on the scrypt gem for hashing, with Base64 and OpenSSL from Ruby's standard library.
13
+
14
+ Use Case: Migrating Firebase Users to Devise
15
+ One common application of Enigma is during user migrations from Firebase to other authentication systems, such as Devise in a Ruby on Rails application. For instance, when importing users from Firebase, you often need to verify their existing passwords without rehashing them prematurely. Enigma shines here by allowing you to:
16
+
17
+ Extract Firebase Data: Pull the user's salt (base64-encoded), stored hash (base64-encoded), and other parameters from Firebase exports.
18
+ Verify Input Password: During login or migration, use Enigma to check if the password the user enters matches the Firebase-stored hash.
19
+ Seamless Transition to Devise: If the verification succeeds, you can safely set the raw password value in Devise (e.g., via user.update(password: raw_password)) to generate a new, Devise-compatible hash. This avoids forcing password resets and provides a smooth user experience.
20
+
21
+ In a real-world migration script, this might look like:
22
+
23
+ ## Installation
24
+
25
+ Replace `enigma` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
26
+
27
+ Install the gem and add to the application's Gemfile by executing:
28
+
29
+ ```bash
30
+ bundle add enigma
31
+ ```
32
+
33
+ If bundler is not being used to manage dependencies, install the gem by executing:
34
+
35
+ ```bash
36
+ gem install enigma
37
+ ```
38
+
39
+ ## Usage
40
+
41
+ # In a Rails app, configure once (e.g., in an initializer)
42
+ Enigma.configure do |config|
43
+ config.base64_signer_key = Rails.application.credentials.dig(:firebase, :base64_signer_key)
44
+ config.base64_salt_separator = Rails.application.credentials.dig(:firebase, :base64_salt_separator)
45
+ config.logger = Rails.logger # Optional: use Rails logger
46
+ end
47
+
48
+ Then verify
49
+ verifier = Enigma::Verifier.new(
50
+ password_to_check: "user_password",
51
+ user_salt_base64: "some_base64_salt",
52
+ stored_hash_base64: "stored_base64_hash"
53
+ )
54
+ verifier.verify # => true or false
55
+
56
+ # Assuming you have Firebase user data
57
+
58
+ firebase_user = { salt_base64: '...', hash_base64: '...', password_input: user_provided_password }
59
+
60
+ Enigma.configure do |config|
61
+ config.base64_signer_key = 'your_firebase_signer_key_base64'
62
+ config.base64_salt_separator = 'your_firebase_salt_separator_base64'
63
+ end
64
+
65
+ verifier = Enigma::Verifier.new(
66
+ password_to_check: firebase_user[:password_input],
67
+ target_salt_base64: firebase_user[:salt_base64],
68
+ stored_hash_base64: firebase_user[:hash_base64]
69
+ )
70
+
71
+ if verifier.verify
72
+ # Password matches! Migrate to Devise
73
+ new_user = User.new(email: firebase_user[:email])
74
+ new_user.password = firebase_user[:password_input] # Set raw password to let Devise hash it
75
+ new_user.save
76
+ puts "User migrated successfully!"
77
+ else
78
+ puts "Password verification failed. Prompt for reset."
79
+ end
80
+
81
+
82
+
83
+ ## Development
84
+
85
+ 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.
86
+
87
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
88
+
89
+ Potential Improvements
90
+
91
+ If this is specifically for Firebase password verification, name the gem something like firebase-scrypt-verifier to make its purpose clear (check rubygems.org for conflicts).
92
+ Add error handling for invalid base64 inputs or scrypt failures.
93
+ If you want to support hashing (not just verification), extend the class with a hash_password method.
94
+ Check for existing gems: A quick search shows gems like firebase-auth-ruby or scrypt, but if this matches Firebase's exact algo (scrypt with AES signing), your custom implementation could fill a niche.
95
+
96
+ If you run into issues during setup or need help with a specific part (e.g., testing), provide more details!
97
+
98
+ ## Contributing
99
+
100
+ Bug reports and pull requests are welcome on GitHub at https://github.com/y-dashev/enigma. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/y-dashev/enigma/blob/master/CODE_OF_CONDUCT.md).
101
+
102
+ ## License
103
+
104
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
105
+
106
+ ## Code of Conduct
107
+
108
+ Everyone interacting in the Enigma project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/y-dashev/enigma/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rubocop/rake_task"
5
+
6
+ RuboCop::RakeTask.new
7
+
8
+ task default: :rubocop
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "scrypt"
4
+ require "base64"
5
+ require "openssl"
6
+ require "logger"
7
+
8
+ module Enigma
9
+ # Verifies the password by comparing it with the stored hash.
10
+ class Verifier
11
+ # @!attribute [r] password_to_check
12
+ # @return [String] The password to be checked.
13
+ # @!attribute [r] target_salt_base64
14
+ # @return [String] The base64-encoded user salt.
15
+ # @!attribute [r] stored_hash_base64
16
+ # @return [String] The base64-encoded stored hash.
17
+ # @!attribute [r] base64_signer_key
18
+ # @return [String] The base64-encoded signer key.
19
+ # @!attribute [r] base64_salt_separator
20
+ # @return [String] The base64-encoded salt separator.
21
+ # @!attribute [r] rounds
22
+ # @return [Integer] The number of rounds used for password hashing.
23
+ # @!attribute [r] mem_cost
24
+ # @return [Integer] The memory cost used for password hashing.
25
+ def initialize(password_to_check:, target_salt_base64:, stored_hash_base64:)
26
+ check_args(password_to_check, target_salt_base64, stored_hash_base64, Enigma.base64_signer_key,
27
+ Enigma.base64_salt_separator,
28
+ Enigma.rounds, Enigma.mem_cost)
29
+
30
+ @password_to_check = password_to_check
31
+ @target_salt_base64 = target_salt_base64
32
+ @stored_hash_base64 = stored_hash_base64
33
+ @base64_signer_key = Enigma.base64_signer_key
34
+ @base64_salt_separator = Enigma.base64_salt_separator
35
+ @rounds = Enigma.rounds
36
+ @mem_cost = Enigma.mem_cost
37
+ end
38
+
39
+ def verify
40
+ verify_password
41
+ end
42
+
43
+ private
44
+
45
+ def check_args(*args)
46
+ args.each do |arg|
47
+ if arg.nil?
48
+ Enigma.logger.error "Argument is nil or missing!"
49
+ raise EnigmaError, "Missing required argument"
50
+ end
51
+ end
52
+ end
53
+
54
+ def verify_password
55
+ user_salt = decoded_user_salt
56
+ salt_separator = decoded_salt_separator
57
+ signer_key = decoded_signer_key
58
+ stored_hash = decoded_stored_hash
59
+ scrypt_salt = user_salt + salt_separator
60
+ scrypt_hash = compute_scrypt_hash(scrypt_salt)
61
+ computed_hash = encrypt_signer_key(scrypt_hash, signer_key)
62
+
63
+ return false if computed_hash.bytesize != stored_hash.bytesize
64
+
65
+ secure_compare(computed_hash, stored_hash)
66
+ end
67
+
68
+ def decoded_user_salt
69
+ Base64.decode64(@target_salt_base64)
70
+ end
71
+
72
+ def decoded_salt_separator
73
+ Base64.decode64(@base64_salt_separator)
74
+ end
75
+
76
+ def decoded_signer_key
77
+ Base64.decode64(@base64_signer_key)
78
+ end
79
+
80
+ def decoded_stored_hash
81
+ Base64.decode64(@stored_hash_base64)
82
+ end
83
+
84
+ def compute_scrypt_hash(scrypt_salt)
85
+ n = 1 << @mem_cost
86
+ r = @rounds
87
+ p = 1
88
+ dk_len = 32
89
+ SCrypt::Engine.scrypt(@password_to_check, scrypt_salt, n, r, p, dk_len)
90
+ end
91
+
92
+ def encrypt_signer_key(scrypt_hash, signer_key)
93
+ iv = "\0" * 16
94
+ cipher = OpenSSL::Cipher.new("aes-256-ctr")
95
+ cipher.encrypt
96
+ cipher.iv = iv
97
+ cipher.key = scrypt_hash
98
+ cipher.update(signer_key) + cipher.final
99
+ end
100
+
101
+ def secure_compare(computed_hash, stored_hash)
102
+ return false unless OpenSSL.respond_to?(:fixed_length_secure_compare)
103
+
104
+ OpenSSL.fixed_length_secure_compare(computed_hash, stored_hash)
105
+ rescue EnigmaError => e
106
+ Enigma.logger.error "Verification failed: #{e.message}. Returning false."
107
+ false
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Enigma
4
+ VERSION = "0.1.0"
5
+ end
data/lib/enigma.rb ADDED
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "enigma/version"
4
+ require_relative "enigma/verifier"
5
+
6
+ # Enigma module provides functionality for encrypting and verifying passwords from scrypt to bcrypt.
7
+ module Enigma
8
+ class << self
9
+ attr_accessor :base64_signer_key, :base64_salt_separator, :rounds, :mem_cost, :logger
10
+
11
+ def configure
12
+ yield self if block_given?
13
+ end
14
+ end
15
+
16
+ self.rounds = 8
17
+ self.mem_cost = 14
18
+ self.logger = Logger.new(STDERR)
19
+
20
+ class EnigmaError < ArgumentError; end
21
+ end
data/sig/enigma.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Enigma
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: enigma-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yavor Dashev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-11-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: scrypt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ description: |
28
+ Enigma is a lightweight Ruby gem designed to verify passwords hashed using Firebase's custom scrypt-based algorithm, making it ideal for seamless integrations and migrations involving Firebase authentication systems. It provides a secure, efficient way to compare a user-provided password against a stored hash without exposing sensitive details, ensuring constant-time comparisons to mitigate timing attacks.
29
+
30
+ Key features include:
31
+ - Full compatibility with Firebase Authentication's password hashing logic, combining scrypt with AES-256-CTR encryption for signing.
32
+ - Configurable parameters for scrypt (rounds, memory cost), signer keys, and salt separators.
33
+ - Secure practices using OpenSSL's fixed-length comparisons.
34
+ - Support for custom logging, with easy integration into Rails or other frameworks.
35
+ - Minimal dependencies, relying on the 'scrypt' gem alongside Ruby's standard library.
36
+
37
+ A common use case is migrating users from Firebase to systems like Devise in Ruby on Rails. During migration, extract the user's base64-encoded salt and stored hash from Firebase, then use Enigma to verify the input password. If it matches, set the raw password in Devise to generate a new hash, avoiding forced resets and ensuring a smooth transition.
38
+
39
+ Whether for custom auth systems, password audits, or hybrid setups, Enigma simplifies secure verification while prioritizing ease of use.
40
+ email:
41
+ - 58559918+y-dashev@users.noreply.github.com
42
+ executables: []
43
+ extensions: []
44
+ extra_rdoc_files: []
45
+ files:
46
+ - ".rubocop.yml"
47
+ - CHANGELOG.md
48
+ - CODE_OF_CONDUCT.md
49
+ - LICENSE.txt
50
+ - README.md
51
+ - Rakefile
52
+ - lib/enigma.rb
53
+ - lib/enigma/verifier.rb
54
+ - lib/enigma/version.rb
55
+ - sig/enigma.rbs
56
+ homepage: https://github.com/y-dashev/enigma
57
+ licenses:
58
+ - MIT
59
+ metadata:
60
+ homepage_uri: https://github.com/y-dashev/enigma
61
+ source_code_uri: https://github.com/y-dashev/enigma
62
+ changelog_uri: https://github.com/y-dashev/enigma/blob/master/CHANGELOG.md
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 3.1.0
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubygems_version: 3.5.16
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: A Ruby gem for verifying passwords hashed with Firebase's scrypt-based algorithm.
82
+ test_files: []