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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:27:09+00:00 2026-06-02T13:27:09+00:00

Background: I am writing a client utility which is capable of connecting to a

  • 0

Background:

I am writing a client utility which is capable of connecting to a remote server using SSL/TLS. The client uses OpenSSL to perform the SSL/TLS transactions and I would like to allow users to specify authorized CA Certs (in the case of self signed certs or private CA setups) used to sign the server’s certificate. I plan on using the cert’s fingerprint, common name, and validity dates to allow the user to quickly view the certs the client uses to validate servers.

Question:

How do you calculate the SHA1 hash/fingerprint of an X509 cert stored within a PEM file using C/C++/Objective-C?

After days of search and experimenting I found a solution and will post it as an answer, however I welcome better or more correct solutions.

  • 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-02T13:27:10+00:00Added an answer on June 2, 2026 at 1:27 pm

    I found below to yield identical output to above:

    +(NSData *)sha1:(SecCertificateRef) cert {
        // fingerprint is over canonical DER rep.
        CFDataRef data = SecCertificateCopyData(cert);
        NSData * out = [[NSData dataWithBytes:CFDataGetBytePtr(data) length:CFDataGetLength(data)] sha1Digest];
        CFRelease(data);
        return out;
    }
    

    which is a bit shorter in objective C. It needs the below extensions to NSData/NSString though to get the formatting close to Netscape, OSX or Windows.

    - (NSData *)md5Digest
    {
        unsigned char result[CC_MD5_DIGEST_LENGTH];
    
        CC_MD5([self bytes], (CC_LONG)[self length], result);
        return [NSData dataWithBytes:result length:CC_MD5_DIGEST_LENGTH];
    }
    
    - (NSData *)sha1Digest
    {
        unsigned char result[CC_SHA1_DIGEST_LENGTH];
    
        CC_SHA1([self bytes], (CC_LONG)[self length], result);
        return [NSData dataWithBytes:result length:CC_SHA1_DIGEST_LENGTH];
    }
    
    - (NSString *)hexStringValue
    {
        NSMutableString *stringBuffer = [NSMutableString stringWithCapacity:([self length] * 2)];
    
        const unsigned char *dataBuffer = [self bytes];
        int i;
    
        for (i = 0; i < [self length]; ++i)
        {
            [stringBuffer appendFormat:@"%02lx", (unsigned long)dataBuffer[i]];
        }
    
        return [stringBuffer copy];
    }
    
    
    - (NSString *)hexColonSeperatedStringValue
    {
        return [self hexColonSeperatedStringValueWithCapitals:YES];
    }
    
    - (NSString *)hexColonSeperatedStringValueWithCapitals:(BOOL)capitalize {
        NSMutableString *stringBuffer = [NSMutableString stringWithCapacity:([self length] * 3)];
    
        const unsigned char *dataBuffer = [self bytes];
        NSString * format = capitalize ? @"%02X" : @"%02x";
        int i;
    
        for (i = 0; i < [self length]; ++i)
        {
            if (i) 
                [stringBuffer appendString:@":"];
            [stringBuffer appendFormat:format, (unsigned long)dataBuffer[i]];
        }
    
        return [stringBuffer copy];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been writing a background service which enforces my client's security policies on company
I am writing a client-server application using TCP Sockets. The server is written in
I'm writing a client-server app using BSD sockets. It needs to run in the
Background I'm writing an web application so I can control an Ubuntu Server from
hey I'm writing on an Server-Client program but when my client sends something, it
I'm currently writing a client-side javascript app which performs image manipulation. Some of the
Background: I am writing a RESTful API on symfony. I want the client to
Some background I am planning to writing a REST service which helps facilitate collaboration
The Problem: Background Information I'm writing an application for a client that is to
Background I am writing a class library assembly in C# .NET 3.5 which is

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.