Exemplo n.º 1
0
/* //////////////////////////////////////////////////////////////////////////////////////
 * implementation
 */
tb_bool_t tb_uuid_make(tb_byte_t uuid[16], tb_char_t const* name)
{
    // check
    tb_assert_and_check_return_val(uuid, tb_false);

    // we only generate it using a simple hashing function for speed if name is supplied 
    tb_bool_t ok = tb_false;
    if (name)
    {
        // generate hash values
        tb_uint32_t h0 = (tb_uint32_t)tb_bkdr_make_from_cstr(name, 'g');
        tb_uint32_t h1 = (tb_uint32_t)tb_bkdr_make_from_cstr(name, 'u');
        tb_uint32_t h2 = (tb_uint32_t)tb_bkdr_make_from_cstr(name, 'i');
        tb_uint32_t h3 = (tb_uint32_t)tb_bkdr_make_from_cstr(name, 'd');

        // fill uuid
        tb_bits_set_u32_be(uuid + 0,    h0);
        tb_bits_set_u32_be(uuid + 4,    h1);
        tb_bits_set_u32_be(uuid + 8,    h2);
        tb_bits_set_u32_be(uuid + 12,   h3);

        // ok
        ok = tb_true;
    }
    else ok = tb_uuid_generate(uuid);

    // ok?
    return ok;
}
Exemplo n.º 2
0
/* we make a fake uuid using random values here.
 *
 * TODO we need a full RFC 4122 4.3 implementation later
 */
static tb_bool_t tb_uuid_generate(tb_byte_t uuid[16])
{
    // disable pseudo random
    tb_random_reset(tb_false);

    // generate random values
    tb_uint32_t r0 = (tb_uint32_t)tb_random();
    tb_uint32_t r1 = (tb_uint32_t)tb_random();
    tb_uint32_t r2 = (tb_uint32_t)tb_random();
    tb_uint32_t r3 = (tb_uint32_t)tb_random();

    // fill uuid
    tb_bits_set_u32_be(uuid + 0,    r0);
    tb_bits_set_u32_be(uuid + 4,    r1);
    tb_bits_set_u32_be(uuid + 8,    r2);
    tb_bits_set_u32_be(uuid + 12,   r3);

    // ok
    return tb_true;
}
Exemplo n.º 3
0
Arquivo: sha.c Projeto: ljx0305/tbox
tb_void_t tb_sha_exit(tb_sha_t* sha, tb_byte_t* data, tb_size_t size)
{
    // check
    tb_assert_and_check_return(sha && data);

    // the count
    tb_hize_t count = tb_bits_be_to_ne_u64(sha->count << 3);

    // spak 
    tb_sha_spak(sha, (tb_byte_t const*)"\200", 1);
    while ((sha->count & 63) != 56) tb_sha_spak(sha, (tb_byte_t const*)"", 1);
    tb_sha_spak(sha, (tb_byte_t*)&count, 8);

    // done
    tb_uint32_t i = 0;
    tb_uint32_t n = sha->digest_len;
    tb_assert((n << 2) <= size);
    for (i = 0; i < n; i++) tb_bits_set_u32_be(data + (i << 2), sha->state[i]);
}