備忘録

備忘録

C++でUUID(v4)を生成する方法

Ⅰ. はじめに

タイトルの通り「C++でUUID(v4)を生成する方法」です。

Ⅱ. やり方(Botanを利用する場合)

1. Botanをインストールする
vcpkg install botan
2. サンプルプログラムを書く
#include <iostream>
#include <botan/uuid.h>
#include <botan/auto_rng.h>

using namespace Botan;

int main()
{
  std::unique_ptr<Botan::RandomNumberGenerator> rng(new Botan::AutoSeeded_RNG);
  auto str = (new Botan::UUID(*rng.get()))->to_string();

  std::cout << str << std::endl;

  // 小文字
  std::transform(str.begin(), str.end(), str.begin(), tolower);
  std::cout << str << std::endl;

  return 0;
}
3. 実行結果
B71A15A6-AA7F-49DA-A6A0-CB05B6C5D17A
b71a15a6-aa7f-49da-a6a0-cb05b6c5d17a

Ⅲ. やり方(Windowsのみ)

1. サンプルプログラムを書く
#include <Windows.h>
#include <string>
#include <iostream>

std::string GuidToString(GUID guid)
{
  // https://gist.github.com/vincenthsu/8fab51834e3a04074a57
  char guid_cstr[37];
  snprintf(guid_cstr, sizeof(guid_cstr),
    "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
    guid.Data1, guid.Data2, guid.Data3,
    guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
    guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);

  return std::string(guid_cstr);
}

int main()
{
  GUID guid;
  auto result = CoCreateGuid(&guid);
  if (result == S_OK)
  {
    std::cout << GuidToString(guid) << std::endl;
  }

  return 0;
}
2. 実行結果
aa55ef25-d7ea-41cc-91b5-68f3e4e24a1e

C++でAESの暗号化と複合を行う方法

Ⅰ. はじめに

タイトルの通り「C++でAESの暗号化と複合を行う方法」です。

Ⅱ. やり方

1. botanをインストールする
vcpkg install botan
2. サンプルプログラムを書く
#include <iostream>
#include <botan/pipe.h>
#include <botan/key_filt.h>
#include <botan/hex_filt.h>
#include <botan/b64_filt.h>
// #include <botan/hex.h>
#include <botan/base64.h>

using namespace Botan;

std::string Encrypt(const std::string str)
{
  // key: "1234567812345678"
  // iv: "8765432187654321"
  // SymmetricKey key(hex_decode("31323334353637383132333435363738"));
  // InitializationVector iv(hex_decode("38373635343332313837363534333231"));
  SymmetricKey key(base64_decode("MTIzNDU2NzgxMjM0NTY3OA=="));
  InitializationVector iv(base64_decode("ODc2NTQzMjE4NzY1NDMyMQ=="));

  // Pipe pipe(get_cipher("AES-128/CBC/PKCS7", key, iv, ENCRYPTION), new Hex_Encoder);
  Pipe pipe(get_cipher("AES-128/CBC/PKCS7", key, iv, ENCRYPTION), new Base64_Encoder);
  pipe.process_msg(str);
  return pipe.read_all_as_string();
}

std::string Decrypt(const std::string str)
{
  // key: "1234567812345678"
  // iv: "8765432187654321"
  // SymmetricKey key(hex_decode("31323334353637383132333435363738"));
  // InitializationVector iv(hex_decode("38373635343332313837363534333231"));
  SymmetricKey key(base64_decode("MTIzNDU2NzgxMjM0NTY3OA=="));
  InitializationVector iv(base64_decode("ODc2NTQzMjE4NzY1NDMyMQ=="));

  // Pipe pipe(new Hex_Decoder, get_cipher("AES-128/CBC/PKCS7", key, iv, DECRYPTION));
  Pipe pipe(new Base64_Decoder, get_cipher("AES-128/CBC/PKCS7", key, iv, DECRYPTION));
  pipe.process_msg(str);
  return pipe.read_all_as_string();
}

int main()
{
  std::string str("Hello World");

  str = Encrypt(str);
  std::cout << str << std::endl;

  str = Decrypt(str);
  std::cout << str << std::endl;

  return 0;
}
実行結果
WbJILhgGP3zOYnFBSPMBow==
Hello World

C++でBase64をエンコード、デコードする方法

Ⅰ. はじめに

タイトルの通り「C++Base64エンコード、デコードする方法」です。

Ⅱ. やり方

1. botanをインストールする
vcpkg install botan
2. サンプルプログラムを書く
#include <iostream>
#include <botan/base64.h>

using namespace Botan;

int main()
{
  std::string str("Hello World");
  str = base64_encode(reinterpret_cast<const uint8_t*>(str.data()), str.size());
  std::cout << str << std::endl;

  auto decoded = base64_decode(str);
  str.assign(decoded.begin(), decoded.end());
  std::cout << str << std::endl;

  return 0;
}
2. サンプルプログラムを書く(Pipeを利用する方法)
#include <iostream>
#include <botan/pipe.h>
#include <botan/b64_filt.h>

using namespace Botan;

std::string Base64Encode(const std::string str)
{
  Pipe pipe(new Base64_Encoder);
  pipe.process_msg(str);
  return pipe.read_all_as_string();
}

std::string Base64Decode(const std::string str)
{
  Pipe pipe(new Base64_Decoder);
  pipe.process_msg(str);
  return pipe.read_all_as_string();
}

int main()
{
  std::string str("Hello World");

  str = Base64Encode(str);
  std::cout << str << std::endl;

  str = Base64Decode(str);
  std::cout << str << std::endl;

  return 0;
}
3. 実行結果
SGVsbG8gV29ybGQ=
Hello World

Visual Studio に英語の言語パックを追加する方法

Ⅰ. はじめに

タイトルの通り「Visual Studio に英語の言語パックを追加する方法」です。

Ⅱ. 英語の言語パックを追加する方法

1. Visual Studio Installer を起動する

f:id:kagasu:20190220163052p:plain

2. 「変更」をクリックする

f:id:kagasu:20190220163330p:plain

3. 「英語」を選択する

f:id:kagasu:20190220163400p:plain

Ⅲ. Visual Studioの言語を英語にする方法

1. Visual Studioを起動する
2. 「ツール」 → 「オプション」をクリックする
3. 英語を選択する

f:id:kagasu:20190220163644p:plain

C/C++ パッケージマネージャvcpkgの使い方

Ⅰ. はじめに

vcpkgはMicrosoftによって開発、メンテナンスされているパッケージマネージャです。
vcpkgはクロスプラットフォームWindows, Linux, macOS)で動作します。

Ⅱ. インストール方法

git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
bootstrap-vcpkg.bat
vcpkg integrate install

Ⅲ. 使い方

パッケージをインストールする
vcpkg install curl
Windows 32bit 動的リンク用のパッケージをインストールする
vcpkg install curl:x86-windows
Windows 64bit 動的リンク用のパッケージをインストールする
vcpkg install curl:x64-windows
Windows 64bit 静的リンク用のパッケージをインストールする
vcpkg install curl:x64-windows-static

※静的リンクの場合、vcxproj ファイルに以下を追加する必要があります

<PropertyGroup Label="Globals">
  <VcpkgTriplet>x64-windows-static</VcpkgTriplet>
</PropertyGroup>

Ⅳ. トラブルシューティング

Q. 以下のエラーが表示され、パッケージのインストールが出来ない。
Warning: The following VS instances are excluded because the English language pack is unavailable.
    C:\Program Files (x86)\Microsoft Visual Studio\2017\Community
Please install the English language pack.
Could not locate a complete toolset.

A. Visual Studioに英語の言語パックを追加する
https://kagasu.hatenablog.com/entry/2019/02/20/163953