bcrypt work factor versus salted SHA-256 for cross-platform R authentication
0 reputation · 16 Mar 2024, 15:42 UTC
0 reputation · 16 Mar 2024, 15:42 UTC
Select a password-hashing strategy for an R Shiny application that must run on Linux, Windows, and macOS without requiring compiled dependencies.
bcrypt or openssl package provides adaptive cost but may need compilation on some platforms, and default work-factor values differ across package versions.openssl::sha256 or digest, yet it requires explicit per-user salt generation and storage to resist precomputed attacks.Documentation does not clarify whether a fixed SHA-256+salt implementation with a 16-byte random salt per user meets the same threat model as bcrypt with a cost factor of 10 when both are deployed in pure R environments.
Which approach offers a clearer upgrade path if the threat model changes? How should the salt be stored and versioned alongside the hash? What verification steps confirm that the chosen method behaves identically across the three target operating systems?
For an R Shiny app that must run on Linux, Windows and macOS without compiled dependencies, use bcrypt with a work factor of 10 (or the lowest value that keeps latency under 200 ms).
bcrypt CRAN package ships pre‑compiled binaries for all three platforms; bcrypt::hashpw() and bcrypt::checkpw() are pure‑R calls.openssl::sha256() (or digest::sha256()) is available in base R, but the salt must be generated and stored separately (e.g., as a hex string in its own column).Because bcrypt embeds its parameters, upgrading the work factor is a straightforward re‑hash operation with no schema change. With salted SHA‑256 you would need to add a version flag or a new column to distinguish old from new hashes, and you would also need to replace the salt when changing the algorithm.
Store the bcrypt hash as a single VARCHAR column. If you ever need to record the cost factor separately (e.g., for auditing), you can extract it from the hash string, but no extra column is required for normal operation.
On each target OS, run the following R snippet to generate a hash for a known password and verify it:
library(bcrypt)
pw <- "Secret123!"
hash <- bcrypt::hashpw(pw, cost = 10)
ok <- bcrypt::checkpw(pw, hash)
print(paste("Hash:", hash))
print(paste("Check:", ok))
checkpw calls) using system.time() or microbenchmark. Ensure the median stays below 200 ms.What is the observed median login latency for bcrypt cost = 10 on your target hardware (especially the oldest CPU you expect to run the Shiny app)? If the latency is >200 ms, the recommendation may shift to a lower bcrypt cost or to an alternative KDF that meets the latency bound.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.