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

  • Home
  • SEARCH
  • 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 6917697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:45:47+00:00 2026-05-27T09:45:47+00:00

I need to generate a hash using HMAC SHA256. I am using the following

  • 0

I need to generate a hash using HMAC SHA256. I am using the following code in Java. I need an equivalent code in Objective-C.

javax.crypto.Mac mac = javax.crypto.Mac.getInstance(type);
javax.crypto.spec.SecretKeySpec secret = new javax.crypto.spec.SecretKeySpec(key.getBytes(), type);
mac.init(secret);
byte[] digest = mac.doFinal(value.getBytes());      
StringBuilder sb = new StringBuilder(digest.length * 2);
String s="";
for (byte b: digest) {
    s = Integer.toHexString(b);
    if (s.length() == 1) {
        sb.append('0');
    }
    sb.append(s);
}
return sb.toString();

Key = YARJSuwP5Oo6/r47LczzWjUx/T8ioAJpUK2YfdI/ZshlTUP8q4ujEVjC0seEUAAtS6YEE1Veghz+IDbNQb+2KQ==

Value =

id=456|time=19:10|nonce=8

Output =

4effffffd8ffffffce7cffffffc4ffffffc71b2f72ffffffdc21ffffffa1ffffffe0ffffffe62d32550b0771296bffffff9c1159ffffffdeffffff8675ffffff9928654c

I have this Objective-C function:

  //Hash method Definition
    - (NSString *)getHashEncription:(NSString *)key andData:(NSString *)data{

        NSLog(@"Secret Key %@ And Data %@", key, data);

        const char *cKey  = [key cStringUsingEncoding:NSASCIIStringEncoding];
        const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding];



        unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];

        //HmacSHA256

        CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);

        NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC
                                              length:sizeof(cHMAC)];

        [Base64 initialize];
        NSString *b64EncStr = [Base64 encode:HMAC];     
        NSLog(@"Base 64 encoded = %@",b64EncStr);   
        NSLog(@"NSData Value %@", HMAC);      

    //    unsigned char hashedChars[32];
    //    NSString *inputString;
    //    inputString = [NSString stringWithFormat:@"hello"];
    //    NSData * inputData = [inputString dataUsingEncoding:NSUTF8StringEncoding];
    //    CC_SHA256(inputData.bytes, inputData.length, hashedChars);


        return [[NSString alloc] initWithData:HMAC encoding:NSASCIIStringEncoding];

    }//End of getHashEncription

The output that I am getting is this:

8736bc4aa7fc3aa071f2b4262b6972a89d2861559a20afa765e46ff17cb181a9

I tried removing the base64 encoding, but it didn’t work.

Any suggestions are most welcome.

  • 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-05-27T09:45:48+00:00Added an answer on May 27, 2026 at 9:45 am

    You need to fix your Java hmac printer, because 4effffffd8ffffffce7cffffffc4ffffffc71b2f72ffffffdc21ffffffa1ffffffe0ffffffe62d32550b0771296bffffff9c1159ffffffdeffffff8675ffffff9928654c isn’t valid. All those ffffff in there are a giveaway that you are sign-extending the bytes to 32-bit signed integers before converting them to hex. Presumably the correct hmac is 4ed8ce7cc4c71b2f72dc21a1e0e62d32550b0771296b9c1159de86759928654c.

    Anyway, I suspect you are calling your method incorrectly. I copied your code into a test program which gives me this output for your key and data:

    2011-12-10 13:03:38.231 hmactest[8251:707] test hmac = <4ed8ce7c c4c71b2f 72dc21a1 e0e62d32 550b0771 296b9c11 59de8675 9928654c>
    

    That matches your desired output (except for the sign-extension errors).

    Here’s my test program:

    #import <Foundation/Foundation.h>
    #import <CommonCrypto/CommonHMAC.h>
    
    NSData *hmacForKeyAndData(NSString *key, NSString *data)
    {
        const char *cKey  = [key cStringUsingEncoding:NSASCIIStringEncoding];
        const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding];
        unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
        CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
        return [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];
    }
    
    int main (int argc, const char * argv[])
    {
        @autoreleasepool {
            // Compare to http://en.wikipedia.org/wiki/HMAC#Examples_of_HMAC_.28MD5.2C_SHA1.2C_SHA256_.29
            NSLog(@"empty hmac = %@", hmacForKeyAndData(@"", @""));
            NSLog(@"test hmac = %@", hmacForKeyAndData(@"YARJSuwP5Oo6/r47LczzWjUx/T8ioAJpUK2YfdI/ZshlTUP8q4ujEVjC0seEUAAtS6YEE1Veghz+IDbNQb+2KQ==", @"id=456|time=19:10|nonce=8"));
        }
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Java to generate the MD5 hash for some files. I need
I need a code example for C#, of how to generate a hash using
I am using the following command in cmd to generate md5 hash for my
I'm trying to generate a fixed length hash using the code below. public int
I need to generate a unique hash code for an object, based on its
I'm using ActiveSupport's to_xml to generate some xml from a hash. I have need
I am currently stumped on recreating an HMAC MD5 hash generated by a Java
Say I'm using a hash to identify files, so I don't need it to
My software is using AES Rijndael. I am using a SHA-256 hash to generate
How to add a hash parameter in link_to. I need to generate a URL

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.