Changeset e0a0a42 for lib


Ignore:
Timestamp:
2012-02-10T13:37:08Z (13 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
bb2d198
Parents:
7d27962
Message:

Added sha1_random_uuid function, which I will use later to generate random
Jabber roomnames.

Location:
lib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • lib/sha1.c

    r7d27962 re0a0a42  
    3737
    3838#include <string.h>
     39#include <stdio.h>
    3940#include "sha1.h"
    4041
     
    421422        sha1_finish( &sha1, Message_Digest );
    422423}
     424
     425/* I think this follows the scheme described on:
     426   http://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_.28random.29
     427   My random data comes from a SHA1 generator but hey, it's random enough for
     428   me, and RFC 4122 looks way more complicated than I need this to be.
     429   
     430   Returns a value that must be free()d. */
     431char *sha1_random_uuid( sha1_state_t * context )
     432{
     433        uint8_t dig[sha1_hash_size];
     434        char *ret = g_new0( char, 40 ); /* 36 chars + \0 */
     435        int i, p;
     436       
     437        sha1_finish(context, dig);
     438        for( p = i = 0; i < 16; i ++ )
     439        {
     440                if( i == 4 || i == 6 || i == 8 || i == 10 )
     441                        ret[p++] = '-';
     442                if( i == 6 )
     443                        dig[i] = ( dig[i] & 0x0f ) | 0x40;
     444                if( i == 8 )
     445                        dig[i] = ( dig[i] & 0x30 ) | 0x80;
     446               
     447                sprintf( ret + p, "%02x", dig[i] );
     448                p += 2;
     449        }
     450        ret[p] = '\0';
     451       
     452        return ret;
     453}
  • lib/sha1.h

    r7d27962 re0a0a42  
    6868G_MODULE_EXPORT int sha1_finish(sha1_state_t *, uint8_t Message_Digest[sha1_hash_size]);
    6969G_MODULE_EXPORT void sha1_hmac(const char *key_, size_t key_len, const char *payload, size_t payload_len, uint8_t Message_Digest[sha1_hash_size]);
     70G_MODULE_EXPORT char *sha1_random_uuid( sha1_state_t * context );
    7071
    7172#endif
Note: See TracChangeset for help on using the changeset viewer.