本文整理汇总了C++中SHA类的典型用法代码示例。如果您正苦于以下问题:C++ SHA类的具体用法?C++ SHA怎么用?C++ SHA使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SHA类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: sha_test
int sha_test()
{
SHA sha;
byte hash[SHA::DIGEST_SIZE];
testVector test_sha[] =
{
testVector("abc",
"\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
"\x6C\x9C\xD0\xD8\x9D"),
testVector("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
"\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
"\xE5\xE5\x46\x70\xF1"),
testVector("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaa",
"\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
"\x2A\x25\xEC\x64\x4D"),
testVector("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaa",
"\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
"\x53\x99\x5E\x26\xA0")
};
int times( sizeof(test_sha) / sizeof(testVector) );
for (int i = 0; i < times; ++i) {
sha.Update(test_sha[i].input_, test_sha[i].inLen_);
sha.Final(hash);
if (memcmp(hash, test_sha[i].output_, SHA::DIGEST_SIZE) != 0)
return -1 - i;
}
return 0;
}
示例2: switch
bool UniqueMUCRoom::handleIqID( Stanza *stanza, int context )
{
switch( stanza->subtype() )
{
case StanzaIqResult:
if( context == RequestUniqueName )
{
Tag *u = stanza->findChild( "unique", XMLNS_MUC_UNIQUE );
if( u )
{
const std::string& name = u->cdata();
if( !name.empty() )
setName( name );
}
}
break;
case StanzaIqError:
if( context == RequestUniqueName )
{
SHA s;
s.feed( m_parent->jid().full() );
s.feed( m_parent->getID() );
setName( s.hex() );
}
break;
default:
break;
}
MUCRoom::join();
return false;
}
示例3: assert
bool DSA::GeneratePrimes(const byte *seedIn, unsigned int g, int &counter,
Integer &p, unsigned int L, Integer &q, bool useInputCounterValue)
{
assert(g%8 == 0);
SHA sha;
SecByteBlock seed(seedIn, g/8);
SecByteBlock U(SHA::DIGESTSIZE);
SecByteBlock temp(SHA::DIGESTSIZE);
SecByteBlock W(((L-1)/160+1) * SHA::DIGESTSIZE);
const int n = (L-1) / 160;
const int b = (L-1) % 160;
Integer X;
sha.CalculateDigest(U, seed, g/8);
for (int i=g/8-1, carry=true; i>=0 && carry; i--)
carry=!++seed[i];
sha.CalculateDigest(temp, seed, g/8);
xorbuf(U, temp, SHA::DIGESTSIZE);
U[0] |= 0x80;
U[SHA::DIGESTSIZE-1] |= 1;
q.Decode(U, SHA::DIGESTSIZE);
if (!IsPrime(q))
return false;
int counterEnd = useInputCounterValue ? counter+1 : 4096;
for (int c = 0; c < counterEnd; c++)
{
for (int k=0; k<=n; k++)
{
for (int i=g/8-1, carry=true; i>=0 && carry; i--)
carry=!++seed[i];
if (!useInputCounterValue || c == counter)
sha.CalculateDigest(W+(n-k)*SHA::DIGESTSIZE, seed, g/8);
}
if (!useInputCounterValue || c == counter)
{
W[SHA::DIGESTSIZE - 1 - b/8] |= 0x80;
X.Decode(W + SHA::DIGESTSIZE - 1 - b/8, L/8);
p = X-((X % (2*q))-1);
if (p.GetBit(L-1) && IsPrime(p))
{
counter = c;
return true;
}
}
}
return false;
}
示例4: Tag
bool SOCKS5BytestreamManager::requestSOCKS5Bytestream( const JID& to, S5BMode /*mode*/,
const std::string& sid )
{
if( !m_parent )
return false;
if( m_hosts.empty() )
{
m_parent->logInstance().log( LogLevelWarning, LogAreaClassS5BManager,
"No stream hosts set, cannot request bytestream." );
return false;
}
const std::string& msid = sid.empty() ? m_parent->getID() : sid;
const std::string& id = m_parent->getID();
Tag *iq = new Tag( "iq" );
iq->addAttribute( "type", "set" );
iq->addAttribute( "to", to.full() );
iq->addAttribute( "id", id );
Tag *q = new Tag( iq, "query", "xmlns", XMLNS_BYTESTREAMS );
q->addAttribute( "sid", msid );
q->addAttribute( "mode", /*( mode == S5BTCP ) ?*/ "tcp" /*: "udp"*/ );
StreamHostList::const_iterator it = m_hosts.begin();
for( ; it != m_hosts.end(); ++it )
{
Tag* s = new Tag( q, "streamhost", "jid", (*it).jid.full() );
s->addAttribute( "host", (*it).host );
s->addAttribute( "port", (*it).port );
}
if( m_server )
{
SHA sha;
sha.feed( msid );
sha.feed( m_parent->jid().full() );
sha.feed( to.full() );
m_server->registerHash( sha.hex() );
}
AsyncS5BItem asi;
asi.sHosts = m_hosts;
asi.id = id;
asi.from = to;
asi.incoming = false;
m_asyncTrackMap[msid] = asi;
m_trackMap[id] = msid;
m_parent->trackID( this, id, S5BOpenStream );
m_parent->send( iq );
return true;
}
示例5: generate
const std::string Capabilities::ver() const
{
if( !m_disco )
return m_ver;
SHA sha;
sha.feed( generate( m_disco->identities(), m_disco->features( true ), m_disco->form() ) );
const std::string& hash = Base64::encode64( sha.binary() );
m_disco->removeNodeHandlers( const_cast<Capabilities*>( this ) );
m_disco->registerNodeHandler( const_cast<Capabilities*>( this ), m_node + '#' + hash );
return hash;
}
示例6:
void SOCKS5Bytestream::setConnectionImpl( ConnectionBase* connection )
{
if( m_socks5 )
delete m_socks5; // deletes m_connection as well
m_connection = connection;
SHA sha;
sha.feed( m_sid );
sha.feed( m_initiator.full() );
sha.feed( m_target.full() );
m_socks5 = new ConnectionSOCKS5Proxy( this, connection, m_logInstance, sha.hex(), 0 );
}
示例7: data
void jAccount::s_saveVCard(VCard *vcard)
{
QString hex = "";
const VCard::Photo &photo = vcard->photo();
if(!photo.binval.empty())
{
QByteArray data(photo.binval.c_str(),photo.binval.length());
SHA sha;
sha.feed(photo.binval);
sha.finalize();
hex = jProtocol::fromStd(sha.hex());
}
m_jabber_protocol->updateAvatarPresence(hex);
m_jabber_protocol->storeVCard(vcard);
}
示例8: Query
NonSaslAuth::Query* NonSaslAuth::Query::newInstance( const std::string& user,
const std::string& sid,
const std::string& pwd,
const std::string& resource ) const
{
Query* q = new Query( user );
if( m_digest && !sid.empty() )
{
SHA sha;
sha.feed( sid );
sha.feed( pwd );
q->m_pwd = sha.hex();
}
else
q->m_pwd = pwd;
q->m_resource = resource;
q->m_digest = m_digest;
return q;
}
示例9: dsa_test
int dsa_test()
{
Source source;
FileSource("../certs/dsa512.der", source);
if (source.size() == 0) {
FileSource("../../certs/dsa512.der", source); // for testsuite
if (source.size() == 0) {
FileSource("../../../certs/dsa512.der", source); // win32 Debug dir
if (source.size() == 0)
err_sys("where's your certs dir?", -89);
}
}
const char msg[] = "this is the message";
byte signature[40];
DSA_PrivateKey priv(source);
DSA_Signer signer(priv);
SHA sha;
byte digest[SHA::DIGEST_SIZE];
sha.Update((byte*)msg, sizeof(msg));
sha.Final(digest);
signer.Sign(digest, signature, rng);
byte encoded[sizeof(signature) + 6];
byte decoded[40];
word32 encSz = EncodeDSA_Signature(signer.GetR(), signer.GetS(), encoded);
DecodeDSA_Signature(decoded, encoded, encSz);
DSA_PublicKey pub(priv);
DSA_Verifier verifier(pub);
if (!verifier.Verify(digest, decoded))
return -90;
return 0;
}
示例10: parent
bool SOCKS5BytestreamManager::requestSOCKS5Bytestream( const JID& to, S5BMode mode,
const std::string& sid,
const JID& from )
{
if( !m_parent )
{
m_parent->logInstance().warn( LogAreaClassS5BManager,
"No parent (ClientBase) set, cannot request bytestream." );
return false;
}
if( m_hosts.empty() )
{
m_parent->logInstance().warn( LogAreaClassS5BManager,
"No stream hosts set, cannot request bytestream." );
return false;
}
const std::string& msid = sid.empty() ? m_parent->getID() : sid;
const std::string& id = m_parent->getID();
IQ iq( IQ::Set, to, id );
iq.addExtension( new Query( msid, mode, m_hosts ) );
if( from )
iq.setFrom( from );
if( m_server )
{
SHA sha;
sha.feed( msid );
if( from )
sha.feed( from.full() );
else
sha.feed( m_parent->jid().full() );
sha.feed( to.full() );
m_server->registerHash( sha.hex() );
}
AsyncS5BItem asi;
asi.sHosts = m_hosts;
asi.id = id;
asi.from = to;
asi.to = from ? from : m_parent->jid();
asi.incoming = false;
m_asyncTrackMap[msid] = asi;
m_trackMap[id] = msid;
m_parent->send( iq, this, S5BOpenStream );
return true;
}
示例11: swap
inline void swap(SHA& a, SHA& b)
{
a.Swap(b);
}
示例12: GetName
// process NAME, either issuer or subject
void CertDecoder::GetName(NameType nt)
{
if (source_.GetError().What()) return;
SHA sha;
word32 length = GetSequence(); // length of all distinguished names
if (length >= ASN_NAME_MAX)
goto err;
length += source_.get_index();
char *ptr, *buf_end;
if (nt == ISSUER) {
ptr= issuer_;
buf_end= ptr + sizeof(issuer_) - 1; // 1 byte for trailing 0
}
else {
ptr= subject_;
buf_end= ptr + sizeof(subject_) - 1; // 1 byte for trailing 0
}
while (source_.get_index() < length) {
GetSet();
GetSequence();
byte b = source_.next();
if (b != OBJECT_IDENTIFIER) {
source_.SetError(OBJECT_ID_E);
return;
}
word32 oidSz = GetLength(source_);
byte joint[2];
memcpy(joint, source_.get_current(), sizeof(joint));
// v1 name types
if (joint[0] == 0x55 && joint[1] == 0x04) {
source_.advance(2);
byte id = source_.next();
b = source_.next(); // strType
word32 strLen = GetLength(source_);
switch (id) {
case COMMON_NAME:
if (!(ptr= AddTag(ptr, buf_end, "/CN=", 4, strLen)))
goto err;
break;
case SUR_NAME:
if (!(ptr= AddTag(ptr, buf_end, "/SN=", 4, strLen)))
goto err;
break;
case COUNTRY_NAME:
if (!(ptr= AddTag(ptr, buf_end, "/C=", 3, strLen)))
goto err;
break;
case LOCALITY_NAME:
if (!(ptr= AddTag(ptr, buf_end, "/L=", 3, strLen)))
goto err;
break;
case STATE_NAME:
if (!(ptr= AddTag(ptr, buf_end, "/ST=", 4, strLen)))
goto err;
break;
case ORG_NAME:
if (!(ptr= AddTag(ptr, buf_end, "/O=", 3, strLen)))
goto err;
break;
case ORGUNIT_NAME:
if (!(ptr= AddTag(ptr, buf_end, "/OU=", 4, strLen)))
goto err;
break;
}
sha.Update(source_.get_current(), strLen);
source_.advance(strLen);
}
else {
bool email = false;
if (joint[0] == 0x2a && joint[1] == 0x86) // email id hdr
email = true;
source_.advance(oidSz + 1);
word32 length = GetLength(source_);
if (email && !(ptr= AddTag(ptr, buf_end, "/emailAddress=", 14, length)))
goto err;
source_.advance(length);
}
}
*ptr= 0;
sha.Final(nt == ISSUER ? issuerHash_ : subjectHash_);
return;
err:
source_.SetError(CONTENT_E);
//.........这里部分代码省略.........
示例13: TEST
TEST(SHATest, sha256Text_a_1m) {
SHA sha;
std::string hash = sha.sha256(boost::filesystem::path("src/test/hash/a_1m.dat"));
ASSERT_EQ("cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0",hash);
}
示例14: re
void NonSaslAuthTest::send( const IQ& iq, IqHandler* ih, int ctx )
{
if( m_test == 1 )
{
const NonSaslAuth::Query* q = iq.findExtension<NonSaslAuth::Query>( ExtNonSaslAuth );
if( q )
{
m_result++;
m_test = 2;
IQ re( IQ::Result, iq.from(), iq.id() );
Tag* d = new Tag( "query" );
d->setXmlns( XMLNS_AUTH );
new Tag( d, "username" );
new Tag( d, "password" );
new Tag( d, "resource" );
re.addExtension( new NonSaslAuth::Query( d ) );
ih->handleIqID( re, ctx );
delete d;
}
}
else if( m_test == 2 )
{
const NonSaslAuth::Query* q = iq.findExtension<NonSaslAuth::Query>( ExtNonSaslAuth );
if( q )
{
Tag* d = q->tag();
if( d->xml() == "<query xmlns='" + XMLNS_AUTH + "'>"
"<username>user</username>"
"<password>pwd</password>"
"<resource>resource</resource>"
"</query>" )
{
m_result++;
IQ re( IQ::Result, iq.from(), iq.id() );
ih->handleIqID( re, ctx );
}
delete d;
}
}
else if( m_test == 3 )
{
const NonSaslAuth::Query* q = iq.findExtension<NonSaslAuth::Query>( ExtNonSaslAuth );
if( q )
{
m_result++;
m_test = 4;
IQ re( IQ::Result, iq.from(), iq.id() );
Tag* d = new Tag( "query" );
d->setXmlns( XMLNS_AUTH );
new Tag( d, "username" );
new Tag( d, "digest" );
new Tag( d, "password" );
new Tag( d, "resource" );
re.addExtension( new NonSaslAuth::Query( d ) );
ih->handleIqID( re, ctx );
delete d;
}
}
else if( m_test == 4 )
{
const NonSaslAuth::Query* q = iq.findExtension<NonSaslAuth::Query>( ExtNonSaslAuth );
if( q )
{
Tag* d = q->tag();
SHA sha;
sha.feed( "sid2" );
sha.feed( "pwd" );
if( d->xml() == "<query xmlns='" + XMLNS_AUTH + "'>"
"<username>user</username>"
"<digest>" + sha.hex() + "</digest>"
"<resource>resource</resource>"
"</query>" )
{
m_result += 2;
IQ re( IQ::Result, iq.from(), iq.id() );
ih->handleIqID( re, ctx );
}
delete d;
}
}
}
示例15: switch
bool NonSaslAuth::handleIqID( Stanza *stanza, int context )
{
switch( stanza->subtype() )
{
case StanzaIqError:
{
m_parent->setAuthed( false );
m_parent->disconnect( ConnAuthenticationFailed );
Tag *t = stanza->findChild( "error" );
if( t )
{
if( t->hasChild( "conflict" ) || t->hasAttribute( "code", "409" ) )
m_parent->setAuthFailure( NonSaslConflict );
else if( t->hasChild( "not-acceptable" ) || t->hasAttribute( "code", "406" ) )
m_parent->setAuthFailure( NonSaslNotAcceptable );
else if( t->hasChild( "not-authorized" ) || t->hasAttribute( "code", "401" ) )
m_parent->setAuthFailure( NonSaslNotAuthorized );
}
break;
}
case StanzaIqResult:
switch( context )
{
case TRACK_REQUEST_AUTH_FIELDS:
{
const std::string& id = m_parent->getID();
Tag *iq = new Tag( "iq" );
iq->addAttribute( "id", id );
iq->addAttribute( "type", "set" );
Tag *query = new Tag( iq, "query" );
query->addAttribute( "xmlns", XMLNS_AUTH );
new Tag( query, "username", m_parent->jid().username() );
new Tag( query, "resource", m_parent->jid().resource() );
Tag *q = stanza->findChild( "query" );
if( ( q->hasChild( "digest" ) ) && !m_sid.empty() )
{
SHA sha;
sha.feed( m_sid );
sha.feed( m_parent->password() );
sha.finalize();
new Tag( query, "digest", sha.hex() );
}
else
{
new Tag( query, "password", m_parent->password() );
}
m_parent->trackID( this, id, TRACK_SEND_AUTH );
m_parent->send( iq );
break;
}
case TRACK_SEND_AUTH:
m_parent->setAuthed( true );
m_parent->connected();
break;
}
break;
default:
break;
}
return false;
}