KHealthCertificate

nlbase45.cpp
1/*
2 * SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
3 * SPDX-License-Identifier: LGPL-2.0-or-later
4 */
5
6#include "nlbase45_p.h"
7
8#include "openssl/bignum_p.h"
9#include "openssl/opensslpp_p.h"
10
11#include <QByteArray>
12#include <QDebug>
13
14#include <algorithm>
15
16static constexpr const char nlBase45Table[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";
17
18static int8_t nlBase45MapFromChar(char c)
19{
20 const auto it = std::find(std::begin(nlBase45Table), std::end(nlBase45Table), c);
21 if (it == std::end(nlBase45Table)) {
22 qWarning() << "invalid base45 character:" << c;
23 return -1;
24 }
25 return std::distance(std::begin(nlBase45Table), it);
26}
27
28QByteArray NLBase45::decode(const char *begin, const char *end)
29{
30 openssl::bn_ptr bn(BN_new());
31 BN_zero(bn.get());
32 for (auto it = begin; it != end; ++it) {
33 BN_mul_word(bn.get(), 45);
34 auto v = nlBase45MapFromChar(*it);
35 if (v < 0) {
36 break;
37 }
38 BN_add_word(bn.get(), v);
39 }
40 return Bignum::toByteArray(bn);
41}
const QList< QKeySequence > & end()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 24 2024 11:51:28 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.