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; }
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); }