Beispiel #1
0
/**
 * Shallow wrapper around uuid, that comes up with some random pseudo
 * uuid if uuid is not available
 */
char *
generateUUID(void)
{
#ifdef HAVE_UUID
# ifdef UUID_PRINTABLE_STRING_LENGTH
	/* Solaris */
	char out[UUID_PRINTABLE_STRING_LENGTH];
# else
	char out[37];
# endif
	uuid_t uuid;
	uuid_generate(uuid);
	uuid_unparse(uuid, out);
#else
	/* try to do some pseudo interesting stuff, and stash it in the
	 * format of a UUID to at least return some uniform answer */
	char out[37];
#ifdef HAVE_OPENSSL
	unsigned char randbuf[16];
	if (RAND_bytes(randbuf, 16) >= 0)
		snprintf(out, sizeof(out),
			 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
			 "%02x%02x%02x%02x%02x%02x",
			 randbuf[0], randbuf[1], randbuf[2], randbuf[3],
			 randbuf[4], randbuf[5], randbuf[6], randbuf[7],
			 randbuf[8], randbuf[9], randbuf[10], randbuf[11],
			 randbuf[12], randbuf[13], randbuf[14], randbuf[15]);
	else
#else
#ifdef HAVE_COMMONCRYPTO
	unsigned char randbuf[16];
	if (CCRandomGenerateBytes(randbuf, 16) == kCCSuccess)
		snprintf(out, sizeof(out),
			 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
			 "%02x%02x%02x%02x%02x%02x",
			 randbuf[0], randbuf[1], randbuf[2], randbuf[3],
			 randbuf[4], randbuf[5], randbuf[6], randbuf[7],
			 randbuf[8], randbuf[9], randbuf[10], randbuf[11],
			 randbuf[12], randbuf[13], randbuf[14], randbuf[15]);
	else
#endif
#endif
		/* generate something like this:
		 * cefa7a9c-1dd2-11b2-8350-880020adbeef
		 * ("%08x-%04x-%04x-%04x-%012x") */
		snprintf(out, sizeof(out),
			 "%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
			 rand() % 65536, rand() % 65536,
			 rand() % 65536, rand() % 65536,
			 rand() % 65536, rand() % 65536,
			 rand() % 65536, rand() % 65536);
#endif
	return strdup(out);
}
Beispiel #2
0
Datei: c4.c Projekt: rhardman/C4
C4Err RNG_GetBytes(     void *         out,
                      size_t         outLen
                      )
{
    C4Err             err = kC4Err_NoErr;

#if _USES_COMMON_CRYPTO_
   
   if(  CCRandomGenerateBytes(out, outLen) != kCCSuccess)
       err =  kC4Err_ResourceUnavailable;
 
#else
    unsigned long count  =  sprng_read(out,outLen,NULL);
    
    if(count != outLen)
        err =  kC4Err_ResourceUnavailable;
#endif
    
    return (err);
    
}
Beispiel #3
0
int32_t mz_crypt_rand(uint8_t *buf, int32_t size)
{
    if (CCRandomGenerateBytes(buf, size) != kCCSuccess)
        return 0;
    return size;
}