CSC 580: Cryptography and Security in Computing

SHA1 Example Code

This page contains examples of using SHA1 in a variety of languages. Note that since HTML requires quoting of certain characters it may or may not work to copy and paste code from this page. To simplify matters, at the top of each code block is a link to the code that you can download (right click, "Save Link As..."). Examples are given below for C, C++, Java, and C#.

Using SHA1 in C or C++

C and C++ do not have cryptographic functions in the standard language and library definitions, but are typically used from the widely-distributed OpenSSL cryptographic library. If your system has the development version of these libraries installed (like the student-accessible UNCG linux host linux.uncg.edu), then you can access the SHA1 functions from C or C++ by including the header file <openssl/sha.h> - when you compile you will have to also tell the compiler to link in the crypto library, so to compile the code below you would use the command:

    gcc sha1-in-c.c -lcrypto

C code: Download Link

#include <stdio.h>
#include <stdlib.h>
#include <openssl/sha.h>

typedef unsigned char byte;

int main(int argc, char *argv[]) {
    const int DataLen = 30;
    SHA_CTX shactx;
    byte digest[SHA_DIGEST_LENGTH];
    int i;

    byte* testdata = (byte *)malloc(DataLen);
    for (i=0; i<DataLen; i++) testdata[i] = 0;

    SHA1_Init(&shactx);
    SHA1_Update(&shactx, testdata, DataLen);
    SHA1_Final(digest, &shactx);

    for (i=0; i<SHA_DIGEST_LENGTH; i++)
	printf("%02x", digest[i]);
    putchar('\n');

    return 0;
}

In C++ the technique is basically identical (you're using the same library, after all), but the command you use is "g++" rather than "gcc". In the code below I actually use the C functions for output since it was easier, but some other parts I converted to C++ style.

C++ code: Download Link

#include <stdio.h>
#include <openssl/sha.h> 

typedef unsigned char byte;

int main(int argc, char *argv[]) {
    const int DataLen = 30;
    SHA_CTX shactx;
    byte digest[SHA_DIGEST_LENGTH];

    byte* testdata = new byte[DataLen];
    for (int i=0; i<DataLen; i++) testdata[i] = 0;

    SHA1_Init(&shactx);
    SHA1_Update(&shactx, testdata, DataLen);
    SHA1_Final(digest, &shactx);

    for (int i=0; i<SHA_DIGEST_LENGTH; i++)
	printf("%02x", digest[i]);
    putchar('\n');

    return 0;
}

SHA1 in Java

Java does have some basic cryptography in the standard library, so the following code should work on any system. Unfortunately, I recently discovered that while the UNCG linux host has a Java runtime environment available, it does not have the compiler installed. This should work on any of the Windows-based lab machines though, through NetBeans or other Java development tools.

Java code: Download Link

import java.util.*;
import java.security.*;

class SHA1_in_Java {
    public static void main(String argv[]) {
	try {
	    MessageDigest md = MessageDigest.getInstance("SHA");
	    byte[] testdata = new byte[30];
	    byte[] digest = md.digest(testdata);

	    for (int i=0; i<digest.length; i++)
		System.out.format("%02x", digest[i]);
	    System.out.println();

	} catch (Exception e) {
	    System.out.println("Exception "+e);
	}
    }
}

SHA1 in C-Sharp

OK, just for fun I decided to see how this works in C#. Please keep in mind that this is the second C# program I have written in my life, and the first one just printed "Hello world"!!! And to top that off, I don't have a Windows machine handy to test this on, so this has only been tested using the Linux mono C# tools. Will it work under Windows? Probably, but that's just a guess....

C# code: Download Link

using System.Security.Cryptography;

public class SHA1_in_CSharp {
    static void Main(string[] args) {
        HashAlgorithm hash = HashAlgorithm.Create("SHA1");
	byte[] input = new byte[30];
	byte[] digest = hash.ComputeHash(input);
        System.Console.WriteLine(System.BitConverter.ToString(digest));
    }
}