Serenity Operating System
at master 38 lines 1.0 kB view raw
1/* 2 * Copyright (c) 2021, Brian Gianforcaro <bgianf@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#pragma once 8 9#include <AK/ByteBuffer.h> 10#include <AK/Noncopyable.h> 11#include <AK/StringView.h> 12 13namespace Core { 14 15class SecretString { 16 AK_MAKE_NONCOPYABLE(SecretString); 17 18public: 19 [[nodiscard]] static ErrorOr<SecretString> take_ownership(char*&, size_t); 20 [[nodiscard]] static SecretString take_ownership(ByteBuffer&&); 21 22 [[nodiscard]] bool is_empty() const { return m_secure_buffer.is_empty(); } 23 [[nodiscard]] size_t length() const { return m_secure_buffer.size(); } 24 [[nodiscard]] char const* characters() const { return reinterpret_cast<char const*>(m_secure_buffer.data()); } 25 [[nodiscard]] StringView view() const { return { characters(), length() }; } 26 27 SecretString() = default; 28 ~SecretString(); 29 SecretString(SecretString&&) = default; 30 SecretString& operator=(SecretString&&) = default; 31 32private: 33 explicit SecretString(ByteBuffer&&); 34 35 ByteBuffer m_secure_buffer; 36}; 37 38}