Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Alexander Afanasyev |
| 5 | * Zhenkai Zhu |
| 6 | * |
| 7 | * BSD license, See the LICENSE file for more information |
| 8 | * |
| 9 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 10 | * Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 11 | */ |
| 12 | |
| 13 | #include "hash.h" |
| 14 | #include "ndn-cpp/helpers/uri.h" |
| 15 | |
| 16 | #include <boost/lexical_cast.hpp> |
| 17 | #include <openssl/evp.h> |
| 18 | #include <fstream> |
| 19 | |
| 20 | using namespace boost; |
| 21 | using namespace std; |
| 22 | |
| 23 | // Other options: VP_md2, EVP_md5, EVP_sha, EVP_sha1, EVP_sha256, EVP_dss, EVP_dss1, EVP_mdc2, EVP_ripemd160 |
| 24 | #define HASH_FUNCTION EVP_sha256 |
| 25 | |
| 26 | namespace ndn |
| 27 | { |
| 28 | |
| 29 | std::ostream & |
| 30 | operator << (std::ostream &os, const Hash &hash) |
| 31 | { |
| 32 | if (hash.m_length == 0) |
| 33 | return os; |
| 34 | |
| 35 | ostreambuf_iterator<char> out_it (os); // ostream iterator |
| 36 | // need to encode to base64 |
| 37 | copy (detail::string_from_binary (reinterpret_cast<const char*> (hash.m_buf)), |
| 38 | detail::string_from_binary (reinterpret_cast<const char*> (hash.m_buf+hash.m_length)), |
| 39 | out_it); |
| 40 | |
| 41 | return os; |
| 42 | } |
| 43 | |
| 44 | std::string |
| 45 | Hash::shortHash () const |
| 46 | { |
| 47 | return lexical_cast<string> (*this).substr (0, 10); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | unsigned char Hash::_origin = 0; |
Jeff Thompson | 2931040 | 2013-06-19 13:32:26 -0700 | [diff] [blame] | 52 | ptr_lib::shared_ptr<Hash> Hash::Origin(new Hash(&Hash::_origin, sizeof(unsigned char))); |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 53 | |
Jeff Thompson | 2931040 | 2013-06-19 13:32:26 -0700 | [diff] [blame] | 54 | ptr_lib::shared_ptr<Hash> |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 55 | Hash::FromString (const std::string &hashInTextEncoding) |
| 56 | { |
Jeff Thompson | 2931040 | 2013-06-19 13:32:26 -0700 | [diff] [blame] | 57 | ptr_lib::shared_ptr<Hash> retval = make_shared<Hash> (reinterpret_cast<void*> (0), 0); |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 58 | |
| 59 | if (hashInTextEncoding.size () == 0) |
| 60 | { |
| 61 | return retval; |
| 62 | } |
| 63 | |
| 64 | if (hashInTextEncoding.size () > EVP_MAX_MD_SIZE * 2) |
| 65 | { |
| 66 | cerr << "Input hash is too long. Returning an empty hash" << endl; |
| 67 | return retval; |
| 68 | } |
| 69 | |
| 70 | retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE]; |
| 71 | |
| 72 | unsigned char *end = copy (detail::string_to_binary (hashInTextEncoding.begin ()), |
| 73 | detail::string_to_binary (hashInTextEncoding.end ()), |
| 74 | retval->m_buf); |
| 75 | |
| 76 | retval->m_length = end - retval->m_buf; |
| 77 | |
| 78 | return retval; |
| 79 | } |
| 80 | |
Jeff Thompson | 2931040 | 2013-06-19 13:32:26 -0700 | [diff] [blame] | 81 | ptr_lib::shared_ptr<Hash> |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 82 | Hash::FromFileContent (const char *filename) |
| 83 | { |
Jeff Thompson | 2931040 | 2013-06-19 13:32:26 -0700 | [diff] [blame] | 84 | ptr_lib::shared_ptr<Hash> retval = make_shared<Hash> (reinterpret_cast<void*> (0), 0); |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 85 | retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE]; |
| 86 | |
| 87 | EVP_MD_CTX *hash_context = EVP_MD_CTX_create (); |
| 88 | EVP_DigestInit_ex (hash_context, HASH_FUNCTION (), 0); |
| 89 | |
| 90 | ifstream iff (filename, std::ios::in | std::ios::binary); |
| 91 | while (iff.good ()) |
| 92 | { |
| 93 | char buf[1024]; |
| 94 | iff.read (buf, 1024); |
| 95 | EVP_DigestUpdate (hash_context, buf, iff.gcount ()); |
| 96 | } |
| 97 | |
| 98 | retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE]; |
| 99 | |
| 100 | EVP_DigestFinal_ex (hash_context, |
| 101 | retval->m_buf, &retval->m_length); |
| 102 | |
| 103 | EVP_MD_CTX_destroy (hash_context); |
| 104 | |
| 105 | return retval; |
| 106 | } |
| 107 | |
Jeff Thompson | 2931040 | 2013-06-19 13:32:26 -0700 | [diff] [blame] | 108 | ptr_lib::shared_ptr<Hash> |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 109 | Hash::FromBytes (const ndn::Bytes &bytes) |
| 110 | { |
Jeff Thompson | 2931040 | 2013-06-19 13:32:26 -0700 | [diff] [blame] | 111 | ptr_lib::shared_ptr<Hash> retval = make_shared<Hash> (reinterpret_cast<void*> (0), 0); |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 112 | retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE]; |
| 113 | |
| 114 | EVP_MD_CTX *hash_context = EVP_MD_CTX_create (); |
| 115 | EVP_DigestInit_ex (hash_context, HASH_FUNCTION (), 0); |
| 116 | |
| 117 | // not sure whether it's bad to do so if bytes.size is huge |
| 118 | EVP_DigestUpdate(hash_context, ndn::head(bytes), bytes.size()); |
| 119 | |
| 120 | retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE]; |
| 121 | |
| 122 | EVP_DigestFinal_ex (hash_context, |
| 123 | retval->m_buf, &retval->m_length); |
| 124 | |
| 125 | EVP_MD_CTX_destroy (hash_context); |
| 126 | |
| 127 | return retval; |
| 128 | } |
| 129 | |
| 130 | } // ndn |