summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCore/SecretString.h
blob: 0e5db04d6e5b74ce89256ec532c12094729d81fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
 * Copyright (c) 2021, Brian Gianforcaro <bgianf@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/ByteBuffer.h>
#include <AK/Noncopyable.h>
#include <AK/StringView.h>

namespace Core {

class SecretString {
    AK_MAKE_NONCOPYABLE(SecretString);

public:
    [[nodiscard]] static ErrorOr<SecretString> take_ownership(char*&, size_t);
    [[nodiscard]] static SecretString take_ownership(ByteBuffer&&);

    [[nodiscard]] bool is_empty() const { return m_secure_buffer.is_empty(); }
    [[nodiscard]] size_t length() const { return m_secure_buffer.size(); }
    [[nodiscard]] char const* characters() const { return reinterpret_cast<char const*>(m_secure_buffer.data()); }
    [[nodiscard]] StringView view() const { return { characters(), length() }; }

    SecretString() = default;
    ~SecretString();
    SecretString(SecretString&&) = default;
    SecretString& operator=(SecretString&&) = default;

private:
    explicit SecretString(ByteBuffer&&);

    ByteBuffer m_secure_buffer;
};

}