Platon Technologies
not logged in Login Registration
EnglishSlovak
open source software development celebrating 10 years of open source development! Friday, March 29, 2024

File: [Platon] / libplaton / utils / cram-md5 / cram-md5.c (download)

Revision 1.2, Fri Dec 26 18:08:16 2008 UTC (15 years, 3 months ago) by nepto


Changes since 1.1: +1 -1 lines

Fixed typo: agains -> against

/*
 * CRAM-MD5 authentication test tool
 * Copyright (C) 2003 Oliver Hitz <oliver@net-track.ch>
 *
 * Compilation against libplaton library:
 *    gcc -pedantic -L ../../platon/ -I ../../ -o cram-md5 cram-md5.c -lplaton
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <platon/hash/hmac.h>
#include <platon/str/base64.h>

int main(int argc, char *argv[])
{
    char *username;
    char *password;
    char *challenge;
    char digest[16];
    char digasc[33];
    int i;
    static char hextab[] = "0123456789abcdef";
    char *decoded;
    char *encoded;
    char *greeting;

    if (argc != 4) {
        fprintf(stderr, "Usage: %s username password auth-greeting\n",
                argv[0]);
        return 1;
    }

    username = argv[1];
    password = argv[2];
    greeting = argv[3];

    challenge = base64_decode_alloc(greeting);
    base64_decode(greeting, challenge);

    printf("username: %s\n", username);
    printf("password: %s\n", password);
    printf("challenge: %s\n", challenge);

    hmac_md5(challenge, strlen(challenge), password, strlen(password), digest);

    digasc[32] = 0;
    for (i = 0; i < 16; i++) {
        digasc[2*i] = hextab[digest[i] >> 4];
        digasc[2*i+1] = hextab[digest[i] & 0xf];
    }

    printf("digest: %s\n", digasc);

    decoded = (char *) malloc(strlen(username) + strlen(digasc) + 2);

    strcpy(decoded, username);
    decoded[strlen(username)] = ' ';
    strcpy(decoded+strlen(username)+1, digasc);
    decoded[strlen(username)+strlen(digasc)+1] = 0;

    printf("base64 decoded: %s\n", decoded);

    encoded = base64_encode_alloc(decoded);
    base64_encode(decoded, encoded);

    printf("base64 encoded: %s\n", encoded);

    return 0;
}

/* Modeline for ViM {{{
 * vim: set ts=4:
 * vim600: fdm=marker fdl=0 fdc=3:
 * }}} */


Platon Group <platon@platon.org> http://platon.org/
Copyright © 2002-2006 Platon Group
Site powered by Metafox CMS
Go to Top