Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-17 07:59:39

0001 // Licensed to the Apache Software Foundation (ASF) under one
0002 // or more contributor license agreements.  See the NOTICE file
0003 // distributed with this work for additional information
0004 // regarding copyright ownership.  The ASF licenses this file
0005 // to you under the Apache License, Version 2.0 (the
0006 // "License"); you may not use this file except in compliance
0007 // with the License.  You may obtain a copy of the License at
0008 //
0009 //   http://www.apache.org/licenses/LICENSE-2.0
0010 //
0011 // Unless required by applicable law or agreed to in writing,
0012 // software distributed under the License is distributed on an
0013 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0014 // KIND, either express or implied.  See the License for the
0015 // specific language governing permissions and limitations
0016 // under the License.
0017 
0018 #pragma once
0019 
0020 #include <cstdint>
0021 #include <string>
0022 
0023 #include "arrow/util/span.h"
0024 #include "arrow/util/visibility.h"
0025 
0026 namespace arrow::util {
0027 /**
0028  * A secure string that ensures the wrapped string is cleared from memory on
0029  * deconstruction. This class can only be created from std::string that are securely
0030  * erased after creation.
0031  *
0032  * Note: This class does not provide a constructor / assignment operator that copies a
0033  * std::string because that would allow code to create a SecureString while accidentally
0034  * not noticing the need to securely erasing the argument after invoking the constructor /
0035  * calling the assignment operator.
0036  */
0037 class ARROW_EXPORT SecureString {
0038  public:
0039   SecureString() = default;
0040   SecureString(SecureString&&) noexcept;
0041   SecureString(const SecureString&) = default;
0042   explicit SecureString(std::string&&) noexcept;
0043   explicit SecureString(size_t, char) noexcept;
0044 
0045   SecureString& operator=(SecureString&&) noexcept;
0046   SecureString& operator=(const SecureString&);
0047   SecureString& operator=(std::string&&) noexcept;
0048 
0049   bool operator==(const SecureString&) const;
0050   bool operator!=(const SecureString&) const;
0051 
0052   ~SecureString() { Dispose(); }
0053 
0054   [[nodiscard]] bool empty() const;
0055   [[nodiscard]] std::size_t size() const;
0056   [[nodiscard]] std::size_t length() const;
0057   [[nodiscard]] std::size_t capacity() const;
0058 
0059   [[nodiscard]] span<uint8_t> as_span();
0060   [[nodiscard]] span<const uint8_t> as_span() const;
0061   [[nodiscard]] std::string_view as_view() const;
0062 
0063   void Dispose();
0064 
0065   static void SecureClear(std::string*);
0066   static void SecureClear(uint8_t* data, size_t size);
0067 
0068  private:
0069   std::string secret_;
0070 };
0071 
0072 }  // namespace arrow::util