All pastes #439499 Raw Edit

Someone

public c v1 · immutable
·published 2007-04-14 09:21 UTC
rendered paste body
#include <stdio.h>#include <sys/ioctl.h>#include <net/if.h>#include <string.h>#include <stdio.h>#include <openssl/evp.h>int main(int argc, char **argv) {    int skfd;    struct ifreq ifr;    EVP_MD_CTX mdctx;    const EVP_MD *md;    unsigned char md_value[EVP_MAX_MD_SIZE];    int md_len, i;    char macbuf[23];    // check the arguments    if (argc != 2) {        printf("Usage: %s eth0\n", argv[0]);        return -1;    }    // create the socket    skfd = socket(PF_INET, SOCK_STREAM, 0);    // make sure we created the socket    if (skfd<0) {        return -1;    }    // copy the name in for the device    strcpy(ifr.ifr_name, argv[1]);    // try and read the mac address for that device    if (ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0) {        return (-1);    }    // get the mac and place in a char buffer    sprintf(macbuf, "hwaddr %.2x:%.2x:%.2x:%.2x:%.2x:%.2x",            (unsigned char) ifr.ifr_hwaddr.sa_data[0],            (unsigned char) ifr.ifr_hwaddr.sa_data[1],            (unsigned char) ifr.ifr_hwaddr.sa_data[2],            (unsigned char) ifr.ifr_hwaddr.sa_data[3],            (unsigned char) ifr.ifr_hwaddr.sa_data[4],            (unsigned char) ifr.ifr_hwaddr.sa_data[5]);    // perhaps this ain't needed    OpenSSL_add_all_digests();    // get the digest    md = EVP_get_digestbyname("sha");    // make sure we can do a digest based on this type    if(!md) {        printf("Meh - cannot do an sha digest");        return -1;    }    // init the digest ctx    EVP_MD_CTX_init(&mdctx);    // init the digest    EVP_DigestInit_ex(&mdctx, md, NULL);    // update the digest with the mac    EVP_DigestUpdate(&mdctx, macbuf, strlen(macbuf));    // get the final digest    EVP_DigestFinal_ex(&mdctx, md_value, &md_len);    // cleanup the digest    EVP_MD_CTX_cleanup(&mdctx);    // print the mac address    printf("Mac is: %s\n", &macbuf);    // print the digest    printf("Digest is: ");    for(i = 0; i < md_len; i++) {        printf("%02x", md_value[i]);    }    printf("\n");    //    return(0);}