int c1s1::s1_create(c1s1* c1)
 {
     int ret = ERROR_SUCCESS;
     
     if (c1->schema == srs_schema_invalid) {
         ret = ERROR_RTMP_CH_SCHEMA;
         srs_error("create s1 failed. invalid schema=%d, ret=%d", c1->schema, ret);
         return ret;
     }
     
     destroy_blocks();
     schema = c1->schema;
     
     time = ::time(NULL);
     version = 0x01000504; // server s1 version
     
     if (schema == srs_schema0) {
         srs_key_block_init(&block0.key);
         srs_digest_block_init(&block1.digest);
         
         // directly generate the public key.
         // @see: https://github.com/winlinvip/simple-rtmp-server/issues/148
         if ((ret = openssl_generate_key(block0.key.key, 128)) != ERROR_SUCCESS) {
             srs_error("calc s1 key failed. ret=%d", ret);
             return ret;
         }
     } else {
         srs_digest_block_init(&block0.digest);
         srs_key_block_init(&block1.key);
         
         // directly generate the public key.
         // @see: https://github.com/winlinvip/simple-rtmp-server/issues/148
         if ((ret = openssl_generate_key(block1.key.key, 128)) != ERROR_SUCCESS) {
             srs_error("calc s1 key failed. ret=%d", ret);
             return ret;
         }
     }
     srs_verbose("calc s1 key success.");
         
     char* s1_digest = NULL;
     if ((ret = calc_s1_digest(s1_digest))  != ERROR_SUCCESS) {
         srs_error("calc s1 digest failed. ret=%d", ret);
         return ret;
     }
     srs_verbose("calc s1 digest success.");
     
     srs_assert(s1_digest != NULL);
     SrsAutoFree(char, s1_digest);
     
     if (schema == srs_schema0) {
         memcpy(block1.digest.digest, s1_digest, 32);
     } else {
         memcpy(block0.digest.digest, s1_digest, 32);
     }
     srs_verbose("copy s1 key success.");
     
     return ret;
 }
int c1s1::s1_create(c1s1* c1,u_int8_t *&shared_key)
{
    int ret = ERROR_SUCCESS;

    if (c1->schema == srs_schema_invalid) {
        ret = ERROR_RTMP_CH_SCHEMA;
        log_error("create s1 failed. invalid schema=%d, ret=%d", c1->schema, ret);
        return ret;
    }

    destroy_blocks();
    schema = c1->schema;

    time = ::time(NULL);
    version = 0x01000504; // server s1 version

    if (schema == srs_schema0) {
        srs_key_block_init(&block0.key);
        srs_digest_block_init(&block1.digest);
    } else {
        srs_digest_block_init(&block0.digest);
        srs_key_block_init(&block1.key);
    }

    if (schema == srs_schema0) {
        if ((ret = openssl_generate_key(c1->block0.key.key, block0.key.key, 128,shared_key)) != ERROR_SUCCESS) {
            log_error("calc s1 key failed. ret=%d", ret);
            return ret;
        }
    } else {
        if ((ret = openssl_generate_key(c1->block1.key.key, block1.key.key, 128,shared_key)) != ERROR_SUCCESS) {
            log_error("calc s1 key failed. ret=%d", ret);
            return ret;
        }
    }
    log_verbose("calc s1 key success.");

    char *s1_digest = NULL;
    if ((ret = calc_s1_digest(s1_digest))  != ERROR_SUCCESS) {
        log_error("calc s1 digest failed. ret=%d", ret);
        return ret;
    }
    log_verbose("calc s1 digest success.");

    mAssert(s1_digest != NULL);
    mAutoFreeArray(char, s1_digest);

    if (schema == srs_schema0) {
        memcpy(block1.digest.digest, s1_digest, 32);
    } else {
        memcpy(block0.digest.digest, s1_digest, 32);
    }
    log_verbose("copy s1 key success.");

    return ret;
}
int c1s1::c1_create(srs_schema_type _schema)
{
    int ret = ERROR_SUCCESS;

    if (_schema == srs_schema_invalid) {
        ret = ERROR_RTMP_CH_SCHEMA;
        log_error("create c1 failed. invalid schema=%d, ret=%d", _schema, ret);
        return ret;
    }

    destroy_blocks();

    time = ::time(NULL);
    // client c1 version
    char *_version = (char *)&version;
    _version[0] = 10;
    _version[1] = 0;
    _version[2] = 45;
    _version[3] = 2;

    if (_schema == srs_schema0) {
        srs_key_block_init(&block0.key);
        srs_digest_block_init(&block1.digest);
    } else {
        srs_digest_block_init(&block0.digest);
        srs_key_block_init(&block1.key);
    }

    schema = _schema;

    char *digest = NULL;

    if ((ret = calc_c1_digest(digest)) != ERROR_SUCCESS) {
        log_error("sign c1 error, failed to calc digest. ret=%d", ret);
        return ret;
    }

    mAssert(digest != NULL);
    mAutoFreeArray(char, digest);

    if (schema == srs_schema0) {
        memcpy(block1.digest.digest, digest, 32);
    } else {
        memcpy(block0.digest.digest, digest, 32);
    }

    return ret;
}
 int c1s1::c1_create(srs_schema_type _schema)
 {
     int ret = ERROR_SUCCESS;
     
     if (_schema == srs_schema_invalid) {
         ret = ERROR_RTMP_CH_SCHEMA;
         srs_error("create c1 failed. invalid schema=%d, ret=%d", _schema, ret);
         return ret;
     }
     
     destroy_blocks();
     
     // client c1 time and version
     time = ::time(NULL);
     version = 0x80000702; // client c1 version
     
     // generate signature by schema
     if (_schema == srs_schema0) {
         srs_key_block_init(&block0.key);
         srs_digest_block_init(&block1.digest);
     } else {
         srs_digest_block_init(&block0.digest);
         srs_key_block_init(&block1.key);
     }
     
     schema = _schema;
     
     // generate digest
     char* digest = NULL;
     
     if ((ret = calc_c1_digest(digest)) != ERROR_SUCCESS) {
         srs_error("sign c1 error, failed to calc digest. ret=%d", ret);
         return ret;
     }
     
     srs_assert(digest != NULL);
     SrsAutoFree(char, digest);
     
     if (schema == srs_schema0) {
         memcpy(block1.digest.digest, digest, 32);
     } else {
         memcpy(block0.digest.digest, digest, 32);
     }
     
     return ret;
 }