blob: aa987840b5e7b191c3dc9d7d924f8de273485e4b [file] [log] [blame]
Jeff Thompsonfa306642013-06-17 15:06:57 -07001/* -*- 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
20using namespace boost;
21using 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
26namespace ndn
27{
28
29std::ostream &
30operator << (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
44std::string
45Hash::shortHash () const
46{
47 return lexical_cast<string> (*this).substr (0, 10);
48}
49
50
51unsigned char Hash::_origin = 0;
Jeff Thompson29310402013-06-19 13:32:26 -070052ptr_lib::shared_ptr<Hash> Hash::Origin(new Hash(&Hash::_origin, sizeof(unsigned char)));
Jeff Thompsonfa306642013-06-17 15:06:57 -070053
Jeff Thompson29310402013-06-19 13:32:26 -070054ptr_lib::shared_ptr<Hash>
Jeff Thompsonfa306642013-06-17 15:06:57 -070055Hash::FromString (const std::string &hashInTextEncoding)
56{
Jeff Thompson29310402013-06-19 13:32:26 -070057 ptr_lib::shared_ptr<Hash> retval = make_shared<Hash> (reinterpret_cast<void*> (0), 0);
Jeff Thompsonfa306642013-06-17 15:06:57 -070058
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 Thompson29310402013-06-19 13:32:26 -070081ptr_lib::shared_ptr<Hash>
Jeff Thompsonfa306642013-06-17 15:06:57 -070082Hash::FromFileContent (const char *filename)
83{
Jeff Thompson29310402013-06-19 13:32:26 -070084 ptr_lib::shared_ptr<Hash> retval = make_shared<Hash> (reinterpret_cast<void*> (0), 0);
Jeff Thompsonfa306642013-06-17 15:06:57 -070085 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 Thompson29310402013-06-19 13:32:26 -0700108ptr_lib::shared_ptr<Hash>
Jeff Thompsonfa306642013-06-17 15:06:57 -0700109Hash::FromBytes (const ndn::Bytes &bytes)
110{
Jeff Thompson29310402013-06-19 13:32:26 -0700111 ptr_lib::shared_ptr<Hash> retval = make_shared<Hash> (reinterpret_cast<void*> (0), 0);
Jeff Thompsonfa306642013-06-17 15:06:57 -0700112 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