blob: c210487d90cb3e95285c644bf5238f33ecb00613 [file] [log] [blame]
deadbeeff137e972017-03-23 15:45:49 -07001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_CRYPTSTRING_H_
12#define RTC_BASE_CRYPTSTRING_H_
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020013
14#include <string.h>
15
16#include <memory>
17#include <string>
18#include <vector>
19
20namespace rtc {
21
22class CryptStringImpl {
Joachim Bauch5b32f232018-03-07 20:02:26 +010023 public:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020024 virtual ~CryptStringImpl() {}
25 virtual size_t GetLength() const = 0;
26 virtual void CopyTo(char * dest, bool nullterminate) const = 0;
27 virtual std::string UrlEncode() const = 0;
28 virtual CryptStringImpl * Copy() const = 0;
29 virtual void CopyRawTo(std::vector<unsigned char> * dest) const = 0;
30};
31
32class EmptyCryptStringImpl : public CryptStringImpl {
Joachim Bauch5b32f232018-03-07 20:02:26 +010033 public:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020034 ~EmptyCryptStringImpl() override {}
35 size_t GetLength() const override;
36 void CopyTo(char* dest, bool nullterminate) const override;
37 std::string UrlEncode() const override;
38 CryptStringImpl* Copy() const override;
39 void CopyRawTo(std::vector<unsigned char>* dest) const override;
40};
41
42class CryptString {
43 public:
44 CryptString();
45 size_t GetLength() const { return impl_->GetLength(); }
Joachim Bauch5b32f232018-03-07 20:02:26 +010046 void CopyTo(char* dest, bool nullterminate) const {
47 impl_->CopyTo(dest, nullterminate);
48 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020049 CryptString(const CryptString& other);
50 explicit CryptString(const CryptStringImpl& impl);
51 ~CryptString();
52 CryptString & operator=(const CryptString & other) {
53 if (this != &other) {
54 impl_.reset(other.impl_->Copy());
55 }
56 return *this;
57 }
58 void Clear() { impl_.reset(new EmptyCryptStringImpl()); }
59 std::string UrlEncode() const { return impl_->UrlEncode(); }
60 void CopyRawTo(std::vector<unsigned char> * dest) const {
61 return impl_->CopyRawTo(dest);
62 }
63
64 private:
65 std::unique_ptr<const CryptStringImpl> impl_;
66};
deadbeeff137e972017-03-23 15:45:49 -070067
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020068class InsecureCryptStringImpl : public CryptStringImpl {
69 public:
70 std::string& password() { return password_; }
71 const std::string& password() const { return password_; }
72
73 ~InsecureCryptStringImpl() override = default;
74 size_t GetLength() const override;
75 void CopyTo(char* dest, bool nullterminate) const override;
76 std::string UrlEncode() const override;
77 CryptStringImpl* Copy() const override;
78 void CopyRawTo(std::vector<unsigned char>* dest) const override;
79
80 private:
81 std::string password_;
82};
83
Joachim Bauch5b32f232018-03-07 20:02:26 +010084} // namespace rtc
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020085
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020086#endif // RTC_BASE_CRYPTSTRING_H_