对信息加密
md5
sha

对信息加密

md5

#include<stdio.h>
#include<openssl/md5.h>
#include<string.h>

int main( int argc, char **argv )
{
    MD5_CTX ctx;
    unsigned char *data="1234";
    unsigned char md[16];
    char buf[33]={'\0'};
    char tmp[3]={'\0'};
    int i;

    MD5_Init(&ctx);
    MD5_Update(&ctx,data,strlen(data));
    MD5_Final(md,&ctx);

    for( i=0; i<16; i++ ){
        sprintf(tmp,"%02X",md[i]);
        strcat(buf,tmp);
    }
    printf("%s\n",buf);
    return 0;
}

sha

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

#include <fcntl.h>

#define BUFFER_SIZE 2048
int main (int argc, char **argv)
{
  SHA_CTX s;
  int bytes_read, from_fd, i;
  unsigned char md[16];
  char buffer[BUFFER_SIZE];

  SHA1_Init(&s);

  if ( argc != 2)
    argv[1] = "/etc/passwd";

  from_fd = open (argv[1], O_RDONLY);
  while (bytes_read=read(from_fd, buffer, BUFFER_SIZE))
    {
      if ((bytes_read==-1))
        break;
      else
        SHA1_Update(&s, buffer, bytes_read);
    }

    SHA1_Final(md, &s);

    for (i=0; i < 20; i++)
      printf ("%.2x", (int)md[i]);

    printf ("\n");

    close(from_fd);

    return 0;
}
# gcc -Wall compute_sha.c -lssl -o compute_sha
# ./compute_sha /var/crepo/other.sqlite.bz2
1df6d3d92693a1114676f8d731448d97163d7791