Skip to content

575: fix double hashing issue#576

Open
nagendra0721 wants to merge 9 commits into
mosip:developfrom
nagendra0721:develop
Open

575: fix double hashing issue#576
nagendra0721 wants to merge 9 commits into
mosip:developfrom
nagendra0721:develop

Conversation

@nagendra0721

@nagendra0721 nagendra0721 commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

[#575]

Summary by CodeRabbit

  • Bug Fixes
    • Improved token ID generation by adjusting the HMAC digest computation process for enhanced consistency and correctness.

Signed-off-by: nagendra0721 <nagendra0718@gmail.com>
Signed-off-by: nagendra0721 <nagendra0718@gmail.com>
Signed-off-by: nagendra0721 <nagendra0718@gmail.com>
Signed-off-by: nagendra0721 <nagendra0718@gmail.com>
Signed-off-by: nagendra0721 <nagendra0718@gmail.com>
Signed-off-by: nagendra0721 <nagendra0718@gmail.com>
Signed-off-by: nagendra0721 <nagendra0718@gmail.com>
@coderabbitai

coderabbitai Bot commented Jun 11, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@nagendra0721, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 49 minutes and 7 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more credits in the billing tab to continue.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: a35197c4-8325-4115-b72c-b888b7ce3592

📥 Commits

Reviewing files that changed from the base of the PR and between cc55c6a and 755427f.

📒 Files selected for processing (1)
  • kernel/kernel-keymanager-service/src/main/java/io/mosip/kernel/tokenidgenerator/generator/TokenIDGenerator.java

Walkthrough

TokenIDGenerator.generateTokenID changes how HMAC digests are computed. Instead of wrapping input bytes with HMACUtils2.generateHash(...) before passing to digestAsPlainText, the method now calls digestAsPlainText directly on concatenated byte arrays for both the intermediate and final hash computations.

Changes

Token ID generation

Layer / File(s) Summary
HMAC digest computation
kernel/kernel-keymanager-service/src/main/java/io/mosip/kernel/tokenidgenerator/generator/TokenIDGenerator.java
generateTokenID modifies how intermediate uinHash and final hash are computed, passing raw concatenated byte arrays directly to HMACUtils2.digestAsPlainText instead of wrapping with generateHash first.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

Suggested reviewers

  • vadherhemant
  • mahammedtaheer

Poem

🐰 A hash upon a hash was once the way,
But nested calls obscured the light of day,
Now bytes flow straight through digest's gentle hand,
The token ID flows forth, both clear and grand! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix double hashing issue' directly references the main code change: removing double hashing in the HMAC computation by calling digestAsPlainText directly instead of wrapping with generateHash first.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
kernel/kernel-keymanager-service/src/main/java/io/mosip/kernel/tokenidgenerator/generator/TokenIDGenerator.java (1)

30-30: 💤 Low value

Potential StringIndexOutOfBoundsException if hash output is shorter than tokenIDLength.

If tokenIDLength exceeds the length of the numeric string derived from hash.getBytes(), substring(0, tokenIDLength) will throw. While this may be unlikely given typical hash lengths and configured token lengths, a defensive check would improve robustness.

🛡️ Defensive fix
-            return new BigInteger(hash.getBytes()).toString().substring(0, tokenIDLength);
+            String numericHash = new BigInteger(hash.getBytes()).toString();
+            return numericHash.substring(0, Math.min(tokenIDLength, numericHash.length()));
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@kernel/kernel-keymanager-service/src/main/java/io/mosip/kernel/tokenidgenerator/generator/TokenIDGenerator.java`
at line 30, The return expression in TokenIDGenerator that does new
BigInteger(hash.getBytes()).toString().substring(0, tokenIDLength) can throw
StringIndexOutOfBoundsException if the numeric string is shorter than
tokenIDLength; fix it in the TokenIDGenerator method by first converting to
String (e.g., String numeric = new BigInteger(hash.getBytes()).toString()), then
if numeric.length() < tokenIDLength pad (e.g., prepend zeros) to reach
tokenIDLength, otherwise return numeric.substring(0, tokenIDLength); update the
return site to use this guarded value so substring never receives an
out-of-range end index.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@kernel/kernel-keymanager-service/src/main/java/io/mosip/kernel/tokenidgenerator/generator/TokenIDGenerator.java`:
- Around line 28-29: The hashing uses String.getBytes() which is
platform-dependent; update TokenIDGenerator where uinHash and hash are computed
to call getBytes with an explicit charset (e.g., StandardCharsets.UTF_8) before
passing to HMACUtils2.digestAsPlainText, and add the necessary import for
java.nio.charset.StandardCharsets so the produced HMACs are deterministic across
platforms.
- Around line 28-29: The token derivation was changed: you removed the
intermediate HMACUtils2.generateHash call which alters downstream Token IDs and
also used platform-default charset via String.getBytes(); to fix, restore the
original intermediate hash step by computing uinHash with
HMACUtils2.generateHash(...) (same method previously used) and then compute the
final hash from that value, and ensure all byte conversions use
StandardCharsets.UTF_8 (e.g., replace getBytes() with
getBytes(StandardCharsets.UTF_8)); look for TokenIDGenerator and the
HMACUtils2.digestAsPlainText/generateHash usages to apply this change so token
outputs remain deterministic and backward-compatible.

---

Nitpick comments:
In
`@kernel/kernel-keymanager-service/src/main/java/io/mosip/kernel/tokenidgenerator/generator/TokenIDGenerator.java`:
- Line 30: The return expression in TokenIDGenerator that does new
BigInteger(hash.getBytes()).toString().substring(0, tokenIDLength) can throw
StringIndexOutOfBoundsException if the numeric string is shorter than
tokenIDLength; fix it in the TokenIDGenerator method by first converting to
String (e.g., String numeric = new BigInteger(hash.getBytes()).toString()), then
if numeric.length() < tokenIDLength pad (e.g., prepend zeros) to reach
tokenIDLength, otherwise return numeric.substring(0, tokenIDLength); update the
return site to use this guarded value so substring never receives an
out-of-range end index.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: dbca8ce2-6628-4a87-8819-8958afed30eb

📥 Commits

Reviewing files that changed from the base of the PR and between 238d38c and cc55c6a.

📒 Files selected for processing (1)
  • kernel/kernel-keymanager-service/src/main/java/io/mosip/kernel/tokenidgenerator/generator/TokenIDGenerator.java

Signed-off-by: nagendra0721 <nagendra0718@gmail.com>
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.

1 participant