lib/gcrypt/MD5: add wrapper in lib/crypto/

Prepare for using other crypto libraries, e.g. FFmpeg's libavutil.
This commit is contained in:
Max Kellermann 2019-08-13 17:20:07 +02:00
parent 2c2efaa91f
commit d515a8e99a
9 changed files with 136 additions and 17 deletions

View File

@ -335,6 +335,8 @@ subdir('src/lib/systemd')
subdir('src/lib/upnp')
subdir('src/lib/yajl')
subdir('src/lib/crypto')
subdir('src/fs')
subdir('src/config')
subdir('src/net')

View File

@ -18,7 +18,7 @@
*/
#include "QobuzClient.hxx"
#include "lib/gcrypt/MD5.hxx"
#include "lib/crypto/MD5.hxx"
#include "util/ConstBuffer.hxx"
#include <stdexcept>

View File

@ -27,7 +27,7 @@
#include "input/FailingInputStream.hxx"
#include "input/InputPlugin.hxx"
#include "config/Block.hxx"
#include "lib/gcrypt/Init.hxx"
#include "lib/crypto/MD5.hxx"
#include "thread/Mutex.hxx"
#include "util/StringCompare.hxx"
@ -123,7 +123,7 @@ QobuzInputStream::OnQobuzTrackError(std::exception_ptr e) noexcept
static void
InitQobuzInput(EventLoop &event_loop, const ConfigBlock &block)
{
Gcrypt::Init();
GlobalInitMD5();
const char *base_url = block.GetBlockValue("base_url",
"http://www.qobuz.com/api.json/0.2/");

View File

@ -42,7 +42,7 @@ qobuz_feature = get_option('qobuz')
if qobuz_feature.disabled()
enable_qobuz = false
else
enable_qobuz = curl_dep.found() and yajl_dep.found() and gcrypt_dep.found()
enable_qobuz = curl_dep.found() and yajl_dep.found() and crypto_md5_dep.found()
if not enable_qobuz and qobuz_feature.enabled()
error('Qobuz requires CURL, libyajl and libgcrypt')
endif
@ -93,7 +93,7 @@ input_plugins = static_library(
nfs_dep,
smbclient_dep,
yajl_dep,
gcrypt_dep,
crypto_md5_dep,
],
)

52
src/lib/crypto/MD5.cxx Normal file
View File

@ -0,0 +1,52 @@
/*
* Copyright 2018-2019 Max Kellermann <max.kellermann@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "MD5.hxx"
#include "lib/gcrypt/MD5.hxx"
#include "lib/gcrypt/Init.hxx"
#include "util/HexFormat.hxx"
void
GlobalInitMD5() noexcept
{
Gcrypt::Init();
}
std::array<uint8_t, 16>
MD5(ConstBuffer<void> input) noexcept
{
return Gcrypt::MD5(input);
}
StringBuffer<33>
MD5Hex(ConstBuffer<void> input) noexcept
{
const auto raw = MD5(input);
return HexFormatBuffer<raw.size()>(&raw.front());
}

51
src/lib/crypto/MD5.hxx Normal file
View File

@ -0,0 +1,51 @@
/*
* Copyright 2018-2019 Max Kellermann <max.kellermann@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef MD5_HXX
#define MD5_HXX
#include "util/StringBuffer.hxx"
#include "util/Compiler.h"
#include <array>
template<typename T> struct ConstBuffer;
void
GlobalInitMD5() noexcept;
gcc_pure
std::array<uint8_t, 16>
MD5(ConstBuffer<void> input) noexcept;
gcc_pure
StringBuffer<33>
MD5Hex(ConstBuffer<void> input) noexcept;
#endif

View File

@ -0,0 +1,18 @@
if gcrypt_dep.found()
crypto_md5 = static_library(
'crypto_md5',
'MD5.cxx',
include_directories: inc,
dependencies: [
gcrypt_dep,
],
)
crypto_md5_dep = declare_dependency(
link_with: crypto_md5,
)
else
crypto_md5_dep = dependency('', required: false)
endif
conf.set('HAVE_MD5', crypto_md5_dep.found())

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 Max Kellermann <max.kellermann@gmail.com>
* Copyright 2018-2019 Max Kellermann <max.kellermann@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@ -29,7 +29,8 @@
#include "MD5.hxx"
#include "Hash.hxx"
#include "util/HexFormat.hxx"
namespace Gcrypt {
std::array<uint8_t, 16>
MD5(ConstBuffer<void> input) noexcept
@ -37,9 +38,4 @@ MD5(ConstBuffer<void> input) noexcept
return Gcrypt::Hash<GCRY_MD_MD5, 16>(input);
}
StringBuffer<33>
MD5Hex(ConstBuffer<void> input) noexcept
{
const auto raw = MD5(input);
return HexFormatBuffer<raw.size()>(&raw.front());
}
} // namespace Gcrypt

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 Max Kellermann <max.kellermann@gmail.com>
* Copyright 2018-2019 Max Kellermann <max.kellermann@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@ -37,12 +37,12 @@
template<typename T> struct ConstBuffer;
namespace Gcrypt {
gcc_pure
std::array<uint8_t, 16>
MD5(ConstBuffer<void> input) noexcept;
gcc_pure
StringBuffer<33>
MD5Hex(ConstBuffer<void> input) noexcept;
} // namespace Gcrypt
#endif