Пример #1
0
INT32 _sptUsrHash::md5( const _sptArguments &arg,
                        _sptReturnVal &rval,
                        bson::BSONObj &detail )
{
    INT32 rc = SDB_OK ;
    stringstream ss ;
    string code ;
    string str ;

    if ( 0 == arg.argc() )
    {
        rc = SDB_INVALIDARG ;
        ss << "not specified the argument" ;
        goto error ;
    }
    rc = arg.getString( 0, str ) ;
    if ( rc )
    {
        PD_LOG ( PDERROR, "failed to get argument: %d", rc ) ;
        ss << "argument \'str\' must be a string" ;
        goto error ;
    }

    code = md5::md5simpledigest( str ) ;
    rval.setStringVal( "", code.c_str() ) ;

done:
    return rc ;
error:
    detail = BSON( SPT_ERR << ss.str() ) ;
    goto done ;
}
Пример #2
0
INT32 _sptUsrHash::fileMD5( const _sptArguments &arg,
                            _sptReturnVal &rval,
                            bson::BSONObj &detail )
{
    INT32 rc = SDB_OK ;
    SINT64 bufSize = SPT_MD5_READ_LEN ;
    SINT64 hasRead = 0 ;
    CHAR readBuf[SPT_MD5_READ_LEN + 1] = { 0 } ;
    OSSFILE file ;
    string filename ;
    stringstream ss ;
    BOOLEAN isOpen = FALSE ;
    md5_state_t st ;
    md5_init( &st ) ;
    md5::md5digest digest ;
    string code ;

    if ( 0 == arg.argc() )
    {
        rc = SDB_INVALIDARG ;
        ss << "not specified the file" ;
        goto error ;
    }
    rc = arg.getString( 0,  filename ) ;
    if ( rc )
    {
        PD_LOG ( PDERROR, "failed to get filename argument: %d", rc ) ;
        ss << "argument \'filename\' must be a string" ;
        goto error ;
    }
    rc = ossOpen( filename.c_str(), OSS_READONLY, 0, file ) ;
    if ( rc )
    {
        ss << "open file[" << filename.c_str() << "] failed: " << rc ;
        goto error ;
    }
    isOpen = TRUE ;

    while ( TRUE )
    {
        rc = ossReadN( &file, bufSize, readBuf, hasRead ) ;
        if ( SDB_EOF == rc || 0 == hasRead )
        {
            rc = SDB_OK ;
            break ;
        }
        else if ( rc )
        {
            ss << "Read file[" << filename.c_str() << "] failed, rc: " << rc ;
            goto error ;
        }
        md5_append( &st, (const md5_byte_t *)readBuf, hasRead ) ;
    }
    md5_finish( &st, digest ) ;
    code = md5::digestToString( digest ) ;
    rval.setStringVal( "", code.c_str() ) ;

done:
    if ( TRUE == isOpen )
        ossClose( file ) ;
    return rc ;
error:
    detail = BSON( SPT_ERR << ss.str() ) ;
    goto done ;
}