Refactor RawBuffer hex dumps
[platform/core/security/key-manager.git] / src / include / ckm / ckm-raw-buffer.h
index d9b41a7..c4c2a06 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2014 Samsung Electronics Co.
+/* Copyright (c) 2014-2019 Samsung Electronics Co., Ltd. All rights reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -113,6 +113,24 @@ struct SafeBuffer {
 // used to pass password and raw key data
 typedef SafeBuffer<unsigned char>::Type RawBuffer;
 
+template <class T, bool Uppercase = false>
+T hexDump(const RawBuffer &raw) {
+       T dump;
+       dump.reserve(2 * raw.size());
+       constexpr char digit[2][16] = {{
+               '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+               'a', 'b', 'c', 'd', 'e', 'f'
+       }, {
+               '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+               'A', 'B', 'C', 'D', 'E', 'F'
+       }};
+       for (auto &e : raw) {
+               dump.push_back(digit[Uppercase][e / 16]);
+               dump.push_back(digit[Uppercase][e % 16]);
+       }
+       return dump;
+}
+
 } // namespace CKM
 
 #endif //_SAFE_BUFFER_H_