Skip to content

Instantly share code, notes, and snippets.

@roa
Created March 29, 2012 10:10
Show Gist options
  • Save roa/2235628 to your computer and use it in GitHub Desktop.
Save roa/2235628 to your computer and use it in GitHub Desktop.
mac to sha1
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <string.h>
#include <openssl/sha.h>
#include <stdlib.h>
#include <unistd.h>
int main( int argc, char *argv[] )
{
int s;
struct ifreq buf;
char mac[20];
char sha1[40];
unsigned char obuf[20];
int i = 0;
memset( &mac , 0, sizeof( mac ) );
memset( &sha1, 0, sizeof( sha1 ) );
memset( &buf , 0x00, sizeof( buf ) );
s = socket( PF_INET, SOCK_DGRAM, 0 );
strcpy( buf.ifr_name, "eth0" );
ioctl( s, SIOCGIFHWADDR, &buf );
close( s );
for( s = 0; s < 6; s++ )
{
char currentHex[2];
snprintf( currentHex, 4, "%.2X", ( unsigned char )buf.ifr_hwaddr.sa_data[s] );
int j;
for( j = 0; j < 2; j++ )
{
mac[i++] = currentHex[j];
}
if( s < 5 )
{
mac[i++] = ':';
}
}
SHA1( ( unsigned char * ) mac, strlen( mac ), obuf );
i = 0;
for ( s = 0; s < 20; s++ )
{
char currentHex[2];
snprintf( currentHex, 4, "%02x ", obuf[s] );
for( int j = 0; j < 2; j++ )
{
sha1[i++] = currentHex[j];
}
}
printf( "%s\n", sha1 );
return 0;
}
@roa
Copy link
Author

roa commented Mar 29, 2012

i have no idea why indentation is broken. :<

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment