~ubuntu-branches/ubuntu/trusty/aria2/trusty-proposed

« back to all changes in this revision

Viewing changes to src/LibgcryptMessageDigestImpl.cc

  • Committer: Package Import Robot
  • Author(s): Kartik Mistry
  • Date: 2013-12-16 18:41:03 UTC
  • mfrom: (2.5.21 sid)
  • Revision ID: package-import@ubuntu.com-20131216184103-xzah3019zwut429g
Tags: 1.18.1-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * aria2 - The high speed download utility
4
4
 *
5
 
 * Copyright (C) 2010 Tatsuhiro Tsujikawa
 
5
 * Copyright (C) 2013 Nils Maier
6
6
 *
7
7
 * This program is free software; you can redistribute it and/or modify
8
8
 * it under the terms of the GNU General Public License as published by
32
32
 * files in the program, then also delete it here.
33
33
 */
34
34
/* copyright --> */
35
 
#include "LibgcryptMessageDigestImpl.h"
36
 
 
37
 
#include <algorithm>
38
 
 
39
 
#include "array_fun.h"
40
 
#include "HashFuncEntry.h"
41
 
#include "a2functional.h"
 
35
 
 
36
#include "MessageDigestImpl.h"
 
37
 
 
38
#include <gcrypt.h>
42
39
 
43
40
namespace aria2 {
44
41
 
45
 
MessageDigestImpl::MessageDigestImpl(int hashFunc) : hashFunc_{hashFunc}
46
 
{
47
 
  gcry_md_open(&ctx_, hashFunc_, 0);
48
 
}
49
 
 
50
 
MessageDigestImpl::~MessageDigestImpl()
51
 
{
52
 
  gcry_md_close(ctx_);
53
 
}
54
 
 
55
 
std::unique_ptr<MessageDigestImpl> MessageDigestImpl::sha1()
56
 
{
57
 
  return make_unique<MessageDigestImpl>(GCRY_MD_SHA1);
58
 
}
59
 
 
60
 
typedef HashFuncEntry<int> CHashFuncEntry;
61
 
typedef FindHashFunc<int> CFindHashFunc;
62
 
 
63
42
namespace {
64
 
CHashFuncEntry hashFuncs[] = {
65
 
  CHashFuncEntry("sha-1", GCRY_MD_SHA1),
66
 
  CHashFuncEntry("sha-224", GCRY_MD_SHA224),
67
 
  CHashFuncEntry("sha-256", GCRY_MD_SHA256),
68
 
  CHashFuncEntry("sha-384", GCRY_MD_SHA384),
69
 
  CHashFuncEntry("sha-512", GCRY_MD_SHA512),
70
 
  CHashFuncEntry("md5", GCRY_MD_MD5)
 
43
template<int hash>
 
44
class MessageDigestBase : public MessageDigestImpl {
 
45
private:
 
46
  struct Deleter {
 
47
    void operator()(gcry_md_hd_t ctx) {
 
48
      if (ctx) {
 
49
        gcry_md_close(ctx);
 
50
      }
 
51
    }
 
52
  };
 
53
 
 
54
public:
 
55
  MessageDigestBase() {
 
56
    gcry_md_hd_t ctx = nullptr;
 
57
    gcry_md_open(&ctx, hash, 0);
 
58
    ctx_.reset(ctx);
 
59
    reset();
 
60
  }
 
61
  virtual ~MessageDigestBase() {}
 
62
 
 
63
  static size_t length() {
 
64
    return ::gcry_md_get_algo_dlen(hash);
 
65
  }
 
66
  virtual size_t getDigestLength() const CXX11_OVERRIDE {
 
67
    return ::gcry_md_get_algo_dlen(hash);
 
68
  }
 
69
  virtual void reset() CXX11_OVERRIDE {
 
70
    ::gcry_md_reset(ctx_.get());
 
71
  }
 
72
  virtual void update(const void* data, size_t length) CXX11_OVERRIDE {
 
73
    auto bytes = reinterpret_cast<const uint8_t*>(data);
 
74
    while (length) {
 
75
      size_t l = std::min(length, (size_t)std::numeric_limits<uint32_t>::max());
 
76
      gcry_md_write(ctx_.get(), bytes, length);
 
77
      length -= l;
 
78
      bytes += l;
 
79
    }
 
80
  }
 
81
  virtual void digest(unsigned char* md) CXX11_OVERRIDE {
 
82
    ::memcpy(md, gcry_md_read(ctx_.get(), 0), getDigestLength());
 
83
  }
 
84
 
 
85
private:
 
86
  std::unique_ptr<std::remove_pointer<gcry_md_hd_t>::type, Deleter> ctx_;
 
87
  size_t len_;
71
88
};
 
89
 
 
90
typedef MessageDigestBase<GCRY_MD_MD5> MessageDigestMD5;
 
91
typedef MessageDigestBase<GCRY_MD_SHA1> MessageDigestSHA1;
 
92
typedef MessageDigestBase<GCRY_MD_SHA224> MessageDigestSHA224;
 
93
typedef MessageDigestBase<GCRY_MD_SHA256> MessageDigestSHA256;
 
94
typedef MessageDigestBase<GCRY_MD_SHA384> MessageDigestSHA384;
 
95
typedef MessageDigestBase<GCRY_MD_SHA512> MessageDigestSHA512;
72
96
} // namespace
73
97
 
74
 
std::unique_ptr<MessageDigestImpl> MessageDigestImpl::create
75
 
(const std::string& hashType)
76
 
{
77
 
  int hashFunc = getHashFunc(std::begin(hashFuncs), std::end(hashFuncs),
78
 
                             hashType);
79
 
  return make_unique<MessageDigestImpl>(hashFunc);
80
 
}
81
 
 
82
 
bool MessageDigestImpl::supports(const std::string& hashType)
83
 
{
84
 
  return std::end(hashFuncs) != std::find_if(std::begin(hashFuncs),
85
 
                                             std::end(hashFuncs),
86
 
                                             CFindHashFunc(hashType));
87
 
}
88
 
 
89
 
size_t MessageDigestImpl::getDigestLength(const std::string& hashType)
90
 
{
91
 
  int hashFunc = getHashFunc(std::begin(hashFuncs), std::end(hashFuncs),
92
 
                             hashType);
93
 
  return gcry_md_get_algo_dlen(hashFunc);
94
 
}
95
 
 
96
 
size_t MessageDigestImpl::getDigestLength() const
97
 
{
98
 
  return gcry_md_get_algo_dlen(hashFunc_);
99
 
}
100
 
 
101
 
void MessageDigestImpl::reset()
102
 
{
103
 
  gcry_md_reset(ctx_);
104
 
}
105
 
void MessageDigestImpl::update(const void* data, size_t length)
106
 
{
107
 
  gcry_md_write(ctx_, data, length);
108
 
}
109
 
 
110
 
void MessageDigestImpl::digest(unsigned char* md)
111
 
{
112
 
  memcpy(md, gcry_md_read(ctx_, 0), gcry_md_get_algo_dlen(hashFunc_));
113
 
}
 
98
std::unique_ptr<MessageDigestImpl> MessageDigestImpl::sha1()
 
99
{
 
100
  return std::unique_ptr<MessageDigestImpl>(new MessageDigestSHA1());
 
101
}
 
102
 
 
103
MessageDigestImpl::hashes_t MessageDigestImpl::hashes = {
 
104
  { "sha-1", make_hi<MessageDigestSHA1>() },
 
105
  { "sha-224", make_hi<MessageDigestSHA224>() },
 
106
  { "sha-256", make_hi<MessageDigestSHA256>() },
 
107
  { "sha-384", make_hi<MessageDigestSHA384>() },
 
108
  { "sha-512", make_hi<MessageDigestSHA512>() },
 
109
  { "md5", make_hi<MessageDigestMD5>() },
 
110
};
114
111
 
115
112
} // namespace aria2