Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7894121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:18:46+00:00 2026-06-03T07:18:46+00:00

I am trying to implement a OTP solution in C# based on RFC 4226:

  • 0

I am trying to implement a OTP solution in C# based on RFC 4226: https://www.rfc-editor.org/rfc/rfc4226

I have found a sample implementation and it looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace OTP
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
            byte[] secretKey = encoding.GetBytes("12345678901234567890");
            byte[] counter = encoding.GetBytes("1");
            Console.WriteLine(CalculateHotp(secretKey, counter));
            Console.ReadKey();
        }

        public static int CalculateHotp(byte[] key, byte[] counter)
        {
            var hmacsha1 = new HMACSHA1(key);
            byte[] hmac_result = hmacsha1.ComputeHash(counter);
            int offset = hmac_result[19] & 0x0f;
            int bin_code = (hmac_result[offset] & 0x7f) << 24
                           | (hmac_result[offset + 1] & 0xff) << 16
                           | (hmac_result[offset + 2] & 0xff) << 8
                           | (hmac_result[offset + 3] & 0xff);
            int hotp = bin_code % 1000000;
            return hotp;
        }
    }
}

The problem is that the call:

byte[] hmac_result = hmacsha1.ComputeHash(counter);

does not return the expected result and thus the returned OTP will be wrong. Reading the RFC4226 appendix D (https://www.rfc-editor.org/rfc/rfc4226#appendix-D), there are some test values to use and the result wont match them:

From the RFC 4226, Appendix D:
The following test data uses the ASCII string
 "12345678901234567890" for the secret:

  Secret = 0x3132333435363738393031323334353637383930

  Table 1 details for each count, the intermediate HMAC value.

Count    Hexadecimal HMAC-SHA-1(secret, count)
0        cc93cf18508d94934c64b65d8ba7667fb7cde4b0
1        75a48a19d4cbe100644e8ac1397eea747a2d33ab
2        0bacb7fa082fef30782211938bc1c5e70416ff44
3        66c28227d03a2d5529262ff016a1e6ef76557ece
4        a904c900a64b35909874b33e61c5938a8e15ed1c
<snip>

Table 2 details for each count the truncated values (both in
hexadecimal and decimal) and then the HOTP value.

                  Truncated
Count    Hexadecimal    Decimal        HOTP
0        4c93cf18       1284755224     755224
1        41397eea       1094287082     287082
2         82fef30        137359152     359152
3        66ef7655       1726969429     969429
4        61c5938a       1640338314     338314
<snip>

Since I in my example above use "12345678901234567890" as key and "1" as counter, I would expect the result of ComputeHash() to be:
75a48a19d4cbe100644e8ac1397eea747a2d33ab
and the OTP to be:
287082

But I get the OTP:
906627

I really cant see what I’m doing wrong here, has anyone successfully implemented a counter based OTP in C# using the HMACSHA1 class?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:18:47+00:00Added an answer on June 3, 2026 at 7:18 am

    You use the counter incorrectly. The counter should not be an ASCII string, it should be a numeric (long) value in big-endian.

    Use

    var counter = new byte[] { 0, 0, 0, 0, 0, 0, 0, 1 };
    

    for this test instead, and your code will return the correct OTP.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to implement an Observer/Observable pattern on an EC2 instance. I have been
Found a really nice code for Accordion nav and am trying implement on our
Trying to implement the excellent jQuery bidirectional infite scroll as explained here: http://www.bennadel.com/blog/1803-Creating-A-Bidirectional-Infinite-Scroll-Page-With-jQuery-And-ColdFusion.htm For
Trying to implement an autocomplete based on this It looks like it's very straight
Trying to implement NSCopying for the first time, and I have a question about
Trying to implement what I thought was a simple concept. I have a user
Trying to implement a simple notification system based on private pub ( something like
trying to implement a multiplayer. Using the sample from Game Center - Sending and
Im trying to implement some code i found on a website which duplicates a
Like the title says, I'm trying to implement the programmatic parts of RFC4226 HOTP:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.