blob: 02d3ccda99835c96817b50d4d5f7a647dad7a550 [file] [log] [blame]
Darin Petkov877642b2011-06-27 13:37:22 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "shill/crypto_rot47.h"
6
7using std::string;
8
9namespace shill {
10
11const char CryptoROT47::kID[] = "rot47";
12
Darin Petkov86964e02011-06-29 13:49:28 -070013CryptoROT47::CryptoROT47() {}
14
Darin Petkov877642b2011-06-27 13:37:22 -070015string CryptoROT47::GetID() {
16 return kID;
17}
18
Paul Stewarta794cd62015-06-16 13:13:10 -070019bool CryptoROT47::Encrypt(const string& plaintext, string* ciphertext) {
Darin Petkov877642b2011-06-27 13:37:22 -070020 const int kRotSize = 94;
21 const int kRotHalf = kRotSize / 2;
22 const char kRotMin = '!';
23 const char kRotMax = kRotMin + kRotSize - 1;
24
25 *ciphertext = plaintext;
Paul Stewarta794cd62015-06-16 13:13:10 -070026 for (auto& ch : *ciphertext) {
Paul Stewart6db7b242014-05-02 15:34:21 -070027 if (ch < kRotMin || ch > kRotMax) {
Darin Petkov877642b2011-06-27 13:37:22 -070028 continue;
29 }
Paul Stewart6db7b242014-05-02 15:34:21 -070030 int rot = ch + kRotHalf;
31 ch = (rot > kRotMax) ? rot - kRotSize : rot;
Darin Petkov877642b2011-06-27 13:37:22 -070032 }
33 return true;
34}
35
Paul Stewarta794cd62015-06-16 13:13:10 -070036bool CryptoROT47::Decrypt(const string& ciphertext, string* plaintext) {
Darin Petkov877642b2011-06-27 13:37:22 -070037 // ROT47 is self-reciprocal.
38 return Encrypt(ciphertext, plaintext);
39}
40
41} // namespace shill