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

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