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 6159259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:06:05+00:00 2026-05-23T21:06:05+00:00

Ruby code: a = OpenSSL::HMAC.digest(‘sha1’, ‘secret’, ‘example’) and then: Base64.encode64(a).chomp yields aMp6Zw1+hHVMmwWXoFp/Aaipc20= iPhone: +

  • 0

Ruby code:

  a = OpenSSL::HMAC.digest('sha1', 'secret', 'example')

and then:

 Base64.encode64(a).chomp

yields

 aMp6Zw1+hHVMmwWXoFp/Aaipc20=

iPhone:

+ (NSData *)hmac:(NSString *)input withKey:(NSString *)key {
    const char *cstrInput = [input cStringUsingEncoding:NSASCIIStringEncoding];
    const char *cstrKey = [key cStringUsingEncoding:NSASCIIStringEncoding];
    NSData *data = [NSData dataWithBytes:cstrInput length:input.length];

    unsigned char chmac[CC_SHA1_DIGEST_LENGTH];
    CCHmac(kCCHmacAlgSHA1, cstrKey, strlen(cstrKey), data, [data length], &chmac);

    NSData *hmacData = [[NSData alloc] initWithBytes:chmac length:sizeof(chmac)];
    return [hmacData autorelease];
}

And

[XICrypto hmac:@"example" withKey:@"secret"];
NSLog(@"HMACData: %@",[HMACData description]);
NSString *HMACEncodedString = [HMACData base64Encoding];

(Where base64Encoding method is from Alex Reynold’s answer on Cocoa Base 64 Implementation for REST Auth)

Results yield

Qm+ManmzmtfhpOzFdf8UHW43L5o=

So these methods are not performing the same operations, why?

EDIT: digest and key were backwards in the Rails call. Fixed that, yet the result is still different from the iPhone’s call.

  • 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-23T21:06:05+00:00Added an answer on May 23, 2026 at 9:06 pm

    Yes I see your comment after I figured out that part on my own :), your rails call is backwards, your next issue is encoding. When ever you want the correct length of bytes when using string encoding use lengthOfBytesUsingEncoding:.

    + (NSData *)hmac:(NSString *)input withKey:(NSString *)key {
        NSStringEncoding encoding = NSASCIIStringEncoding;
        const char *cstrInput = [input cStringUsingEncoding:encoding];
        NSUInteger inputLength = [input lengthOfBytesUsingEncoding:encoding];
        const char *cstrKey = [key cStringUsingEncoding:encoding];
        NSUInteger keyLength = [key lengthOfBytesUsingEncoding:encoding];
    
        unsigned char chmac[CC_SHA1_DIGEST_LENGTH];
        CCHmac(kCCHmacAlgSHA1, cstrKey, keyLength, cstrInput, inputLength, &chmac);
    
        return [[[NSData alloc] initWithBytes:chmac length:sizeof(chmac)] autorelease];
    }
    

    I logged the contents of the data and get 68ca7a67 0d7e8475 4c9b0597 a05a7f01 a8a9736d and if you paste that into the hex field of this translator you will get aMp6Zw1+hHVMmwWXoFp/Aaipc20= in the base64 field.

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

Sidebar

Related Questions

Are there certain code conventions when documenting ruby code? For example I have the
I have some Ruby code which takes dates on the command line in the
Consider the following ruby code test.rb: begin puts thisFunctionDoesNotExist x = 1+1 rescue Exception
Here's some Ruby code: puts %x{ pstree #{$$} } # never forks puts %x{
Given the Ruby code line = first_name=mickey;last_name=mouse;country=usa record = Hash[*line.split(/=|;/)] I understand everything in
I've written some Ruby code to inspect ZIP-files as part of an internal company
is there some ruby code I can use to install a gem from a
I am writing some Ruby code, not Rails, and I need to handle something
Say I'm writing some ruby code and I want to use the standard Date
Let's say I have the following ruby code : def use_object(object) puts object.some_method end

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.