示例#1
0
void SMTPClientSession::loginUsingCRAM(const std::string& username, const std::string& method, Poco::DigestEngine& hmac)
{
	std::string response;
	int status = sendCommand(std::string("AUTH ") + method, response);

	if (!isPositiveIntermediate(status)) throw SMTPException(std::string("Cannot authenticate using ") + method, response, status);
	std::string challengeBase64 = response.substr(4);
	
	std::istringstream istr(challengeBase64);
	Base64Decoder decoder(istr);
	std::string challenge;
	StreamCopier::copyToString(decoder, challenge);
	
	hmac.update(challenge);
	
	const DigestEngine::Digest& digest = hmac.digest();
	std::string digestString(DigestEngine::digestToHex(digest));
	
	std::string challengeResponse = username + " " + digestString;
	
	std::ostringstream challengeResponseBase64;
	Base64Encoder encoder(challengeResponseBase64);
	encoder.rdbuf()->setLineLength(0);
	encoder << challengeResponse;
	encoder.close();
	
	status = sendCommand(challengeResponseBase64.str(), response);
  	if (!isPositiveCompletion(status)) throw SMTPException(std::string("Login using ") + method + " failed", response, status);  
}
示例#2
0
void Codec::DigestToHex(const ValueList& args, KValueRef result)
{
    args.VerifyException("digestToHex", "i s|o");
    
    int type = args.at(0)->ToInt();
    
    Poco::DigestEngine *engine = NULL;
    
    switch(type)
    {
        case CODEC_MD2:
        {
            engine = new Poco::MD2Engine(); 
            break;
        }
        case CODEC_MD4:
        {
            engine = new Poco::MD4Engine(); 
            break;
        }
        case CODEC_MD5:
        {
            engine = new Poco::MD5Engine(); 
            break;
        }
        case CODEC_SHA1:
        {
            engine = new Poco::SHA1Engine(); 
            break;
        }
        default:
        {
            std::ostringstream msg("Unsupported encoding type: ");
            msg << type;
            throw ValueException::FromString(msg.str());
        }
    }
    
    if (args.at(1)->IsString())
    {
        engine->update(args.GetString(1));
    }
    else
    {
        AutoPtr<Bytes> bytes(args.GetObject(1).cast<Bytes>());
        if (!bytes.isNull())
        {
            engine->update(bytes->Pointer(), bytes->Length());
        }
    }
    std::string data = Poco::DigestEngine::digestToHex(engine->digest()); 
    result->SetString(data);
    delete engine;
}
std::string SCRAMAuthenticator::digestToHexString(Poco::DigestEngine& engine)
{
	Poco::DigestEngine::Digest d = engine.digest();
	return Poco::DigestEngine::digestToHex(d);
}
std::string SCRAMAuthenticator::digestToBinaryString(Poco::DigestEngine& engine)
{
	Poco::DigestEngine::Digest d = engine.digest();
	return std::string(reinterpret_cast<const char*>(&d[0]), d.size());
}