Skip to content

Some hashing refactoring#184

Merged
hhvrc merged 13 commits intodevelopfrom
feature/sha-hash-cryptrand-serverside-tokens
Apr 20, 2025
Merged

Some hashing refactoring#184
hhvrc merged 13 commits intodevelopfrom
feature/sha-hash-cryptrand-serverside-tokens

Conversation

@hhvrc
Copy link
Copy Markdown
Contributor

@hhvrc hhvrc commented Apr 20, 2025

Reason for changing secret hashing:

Server generated tokens have a high entropy due to being random cryptographically secure strings, they do not suffer from the same pitfalls which human-made low entropy passwords do.
This makes using password-hashing API's on them which might get more expensive in the future overkill and unnecessary.

see:
https://security.stackexchange.com/questions/232753/is-single-iteration-sha-256-safe-enough-for-hashing-128bit-random-number-api-key
https://stackoverflow.com/questions/76189626/which-hashing-algorithm-to-use-for-api-key

hhvrc added 4 commits April 20, 2025 13:29
Server generated tokens have a high entropy due to being random cryptographically secure strings, they do not suffer from the same pitfalls which human-made low entropy passwords do.
This makes using password-hashing API's on them which might get more expensive in the future overkill and unnecessary.
@hhvrc hhvrc requested a review from LucHeart April 20, 2025 11:42
@hhvrc hhvrc self-assigned this Apr 20, 2025
@hhvrc hhvrc merged commit 3964f10 into develop Apr 20, 2025
5 checks passed
@hhvrc hhvrc deleted the feature/sha-hash-cryptrand-serverside-tokens branch April 20, 2025 23:56
@LucHeart LucHeart requested a review from Copilot April 20, 2025 23:59
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors token and secret handling to avoid unnecessary hashing on high entropy, server‑generated strings. Key changes include renaming session-related methods to better indicate token usage, updating secret/secret property names to secretHash across the codebase, and switching to a unified hashing utility and token length defined in AuthConstants.

Reviewed Changes

Copilot reviewed 23 out of 23 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
Common/Utils/AuthUtils.cs Renamed TryGetUserSession to TryGetUserSessionToken for clarity
Common/Services/Session/SessionService.cs Updated session creation and retrieval to use CreateSessionResult and consistent token handling
Common/Services/BatchUpdate/BatchUpdateService.cs Refactored token update mechanism with the new token length and hashing logic
Common/OpenShockDb/* Replaced Secret properties with SecretHash to align with the new security approach
API/Controller/* and Authentication Handlers Updated methods to use token-based session and API token authentication

Comment on lines +53 to +57
// Only hash new tokens, old ones are 64 chars long
if (sessionToken.Length == AuthConstants.GeneratedTokenLength)
{
sessionToken = HashingUtils.HashToken(sessionToken);
}
Copy link

Copilot AI Apr 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The conditional token hashing logic here is duplicated in other parts of the codebase. Consider extracting a shared utility method for normalizing tokens to ensure consistent behavior.

Suggested change
// Only hash new tokens, old ones are 64 chars long
if (sessionToken.Length == AuthConstants.GeneratedTokenLength)
{
sessionToken = HashingUtils.HashToken(sessionToken);
}
// Normalize the session token
sessionToken = NormalizeToken(sessionToken);

Copilot uses AI. Check for mistakes.
{
_sessionLastUsed.Enqueue(sessionKey, lastUsed);
// Only hash new tokens, old ones are 64 chars long
if (sessionToken.Length == AuthConstants.GeneratedTokenLength)
Copy link

Copilot AI Apr 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Token normalization by checking the token length is repeated here; centralizing this logic in a utility method would reduce maintenance risk and ensure consistency.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants