const char* websocket_derive_key(const char* key){ static char hex[512] = {0,}; static char magic[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; /* compute concaternated hash */ sha1_t sha = sha1_new(); sha1_update(sha, key, strlen(key)); sha1_update(sha, magic, strlen(magic)); /* encode hash as base64 */ base64encode(sha1_hash_bytes(sha), 20, hex, sizeof(hex)); sha1_free(sha); return hex; }
/* * SHA-1 HMAC context setup */ void sha1_hmac_starts( sha1_context *ctx, const unsigned char *key, size_t keylen ) { if( keylen > 64 ) // Key shortened if greater than BLOCK SIZE (64 bytes) { sha1_context cty; sha1_init(&cty); sha1_starts(&cty); sha1_update(&cty, key, keylen); // Get the 20 char digest of the key sha1_finish(&cty, ctx->key); // Store the digest in ctx.key sha1_free(&cty); ctx->keyLen = 20; } else { ctx->keyLen = keylen; // Use the key as is memcpy(ctx->key, key, keylen); // Store the key in ctx.key } }
static void sha1_ctx_free( void *ctx ) { sha1_free( (sha1_context *) ctx ); polarssl_free( ctx ); }