http://en.wikipedia.org/wiki/SHA-1
Example hashes
Main article: Examples of SHA digests
The following is an example of SHA-1 digests. ASCII encoding is assumed for all messages.
[code]
SHA1("The quick brown fox jumps over the lazy dog")
= 2fd4e1c6 7a2d28fc ed849ee1 bb76e739 1b93eb12
[/code]
Even a small change in the message will, with overwhelming probability, result in a completely different hash due to the avalanche effect. For example, changing dog to cog produces a hash with different values for 81 of the 160 bits:
[code]
SHA1("The quick brown fox jumps over the lazy cog")
= de9f2c7f d25e1b3a fad3e85a 0bd17d9b 100db4b3
[/code]
[code]
/**
* @file CSha1.cpp
* @author cheol-dong choi <aucd29@gmail.com>
* @version 1.0
* @date 2010-09-28 (17:20:34)
* @section LICENSE
*
* Copyright (c) 2003-2010, cheol-dong choi, (http://www.sarangnamu.net)
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* @section DESCRIPTION
*
*
* @section CHANGE_LOG
*
*/
#include "CSha1.h"
#include "RSLOGGER.H"
#include "openssl/evp.h"
#include "openssl/sha.h"
#include "Utility.h"
/*
* Constructor CSha1
*/
CSha1::CSha1()
{
LOG1("CSha1::CSha1\n");
}
/*
* Destructor ~CSha1
*/
CSha1::~CSha1()
{
LOG1("CSha1::~CSha1\n");
}
/**
*
*
* @param plainText
* @return
*/
std::string CSha1::toSHA1(const char* plainText)
{
LOG1("CSha1::toSHA1\n");
unsigned char t1[20] = {0};
unsigned char t2[24] = {0};
SHA1((const unsigned char*)plainText, strlen(plainText), t1);
SHA_CTX ctx;
SHA1_Init(&ctx);
SHA1_Update(&ctx, plainText, strlen(plainText));
SHA1_Final(t2, &ctx);
CUtility util;
std::string res;
util.Bin2Hex(t2, res);
return res;
}
[/code]
[code]
/**
* @file CSha1.h
* @author cheol-dong choi <aucd29@gmail.com>
* @version 1.0
* @date 2010-09-28 (17:20:44)
* @section LICENSE
*
* Copyright (c) 2003-2010, cheol-dong choi, (http://www.sarangnamu.net)
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* @section DESCRIPTION
*
*
* @section CHANGE_LOG
*
*/
#ifndef __CD_CSHA1_H__
#define __CD_CSHA1_H__
#include <string>
/**
* @class CSha1
* @brief
*/
class CSha1
{
public:
/*
* Construction
*/
CSha1();
/*
* Destructor
*/
virtual ~CSha1();
public:
/*
* Getter methods
*/
static std::string toSHA1(const char* plainText);
protected:
/*
* Internal methods
*/
public:
/*
* Attributes
*/
};
#endif
[/code]
Example hashes
Main article: Examples of SHA digests
The following is an example of SHA-1 digests. ASCII encoding is assumed for all messages.
[code]
SHA1("The quick brown fox jumps over the lazy dog")
= 2fd4e1c6 7a2d28fc ed849ee1 bb76e739 1b93eb12
[/code]
Even a small change in the message will, with overwhelming probability, result in a completely different hash due to the avalanche effect. For example, changing dog to cog produces a hash with different values for 81 of the 160 bits:
[code]
SHA1("The quick brown fox jumps over the lazy cog")
= de9f2c7f d25e1b3a fad3e85a 0bd17d9b 100db4b3
[/code]
[code]
/**
* @file CSha1.cpp
* @author cheol-dong choi <aucd29@gmail.com>
* @version 1.0
* @date 2010-09-28 (17:20:34)
* @section LICENSE
*
* Copyright (c) 2003-2010, cheol-dong choi, (http://www.sarangnamu.net)
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* @section DESCRIPTION
*
*
* @section CHANGE_LOG
*
*/
#include "CSha1.h"
#include "RSLOGGER.H"
#include "openssl/evp.h"
#include "openssl/sha.h"
#include "Utility.h"
/*
* Constructor CSha1
*/
CSha1::CSha1()
{
LOG1("CSha1::CSha1\n");
}
/*
* Destructor ~CSha1
*/
CSha1::~CSha1()
{
LOG1("CSha1::~CSha1\n");
}
/**
*
*
* @param plainText
* @return
*/
std::string CSha1::toSHA1(const char* plainText)
{
LOG1("CSha1::toSHA1\n");
unsigned char t1[20] = {0};
unsigned char t2[24] = {0};
SHA1((const unsigned char*)plainText, strlen(plainText), t1);
SHA_CTX ctx;
SHA1_Init(&ctx);
SHA1_Update(&ctx, plainText, strlen(plainText));
SHA1_Final(t2, &ctx);
CUtility util;
std::string res;
util.Bin2Hex(t2, res);
return res;
}
[/code]
[code]
/**
* @file CSha1.h
* @author cheol-dong choi <aucd29@gmail.com>
* @version 1.0
* @date 2010-09-28 (17:20:44)
* @section LICENSE
*
* Copyright (c) 2003-2010, cheol-dong choi, (http://www.sarangnamu.net)
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* @section DESCRIPTION
*
*
* @section CHANGE_LOG
*
*/
#ifndef __CD_CSHA1_H__
#define __CD_CSHA1_H__
#include <string>
/**
* @class CSha1
* @brief
*/
class CSha1
{
public:
/*
* Construction
*/
CSha1();
/*
* Destructor
*/
virtual ~CSha1();
public:
/*
* Getter methods
*/
static std::string toSHA1(const char* plainText);
protected:
/*
* Internal methods
*/
public:
/*
* Attributes
*/
};
#endif
[/code]
'Native > C++' 카테고리의 다른 글
unit test tools (0) | 2013.10.02 |
---|---|
mutex , critical section, semaphore (0) | 2013.10.02 |
adding color to vim and cygwin (0) | 2013.10.02 |
pkcs padding (0) | 2013.10.02 |
Block cipher modes of operation (0) | 2013.10.02 |