File indexing completed on 2024-12-29 04:50:58

0001 /*
0002  * SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
0003  * SPDX-License-Identifier: LGPL-2.0-or-later
0004  */
0005 
0006 #ifndef KITINERARY_BIGNUM_P_H
0007 #define KITINERARY_BIGNUM_P_H
0008 
0009 #include "opensslpp_p.h"
0010 
0011 #include <QByteArray>
0012 
0013 /** Utilities for working with OpenSSL BIGNUM objects. */
0014 class Bignum
0015 {
0016 public:
0017     /** @see BN_bin2bn */
0018     static inline openssl::bn_ptr fromByteArray(const uint8_t *bin, std::size_t size)
0019     {
0020         return openssl::bn_ptr(BN_bin2bn(bin, size, nullptr));
0021     }
0022     static inline openssl::bn_ptr fromByteArray(const char *bin, std::size_t size)
0023     {
0024         return openssl::bn_ptr(BN_bin2bn(reinterpret_cast<const uint8_t*>(bin), size, nullptr));
0025     }
0026     static inline openssl::bn_ptr fromByteArray(const QByteArray &bin)
0027     {
0028         return fromByteArray(bin.constData(), bin.size());
0029     }
0030 
0031     /** @see BN_bn2bin */
0032     static inline QByteArray toByteArray(const openssl::bn_ptr &bn)
0033     {
0034         QByteArray bin;
0035         bin.resize(BN_num_bytes(bn.get()));
0036         BN_bn2bin(bn.get(), reinterpret_cast<uint8_t*>(bin.data()));
0037         return bin;
0038     }
0039 };
0040 
0041 #endif
0042