Home | History | Annotate | Download | only in util
      1 // Copyright 2012 Google Inc. All Rights Reserved.
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 #ifndef POLO_UTIL_POLOUTIL_H_
     16 #define POLO_UTIL_POLOUTIL_H_
     17 
     18 #include <openssl/ssl.h>
     19 #include <stdint.h>
     20 #include <string>
     21 #include <vector>
     22 
     23 namespace polo {
     24 namespace util {
     25 
     26 // Utilites used for the Polo protocol.
     27 class PoloUtil {
     28  public:
     29   // Converts an array of big-endian bytes to a hex string.
     30   // @param bytes an array of big-endian bytes
     31   // @param length the length of the given byte array
     32   // @return a hex string representing the given byte array
     33   static const std::string BytesToHexString(const uint8_t* bytes,
     34                                             size_t length);
     35 
     36   // Converts a hex string to an array of big-endian bytes. A new byte array
     37   // is created at the given bytes pointer, and the number of bytes is returned.
     38   // The byte array must be freed using: delete[] bytes.
     39   // @param hex_string the hex string to convert
     40   // @param bytes pointer to a byte array that will be created with the
     41   //              big-endian result
     42   // @return the number of bytes in the resulting byte array
     43   static const size_t HexStringToBytes(const std::string hex_string,
     44                                        uint8_t*& bytes);
     45 
     46   // Converts an integer value to a big-endian array of bytes. A new byte array
     47   // is created at the given bytes pointer. There are always 4 bytes in the
     48   // array. The byte array must be freed using: delete[] bytes.
     49   // @param value the integer value to convert
     50   // @param bytes pointer to a byte array that will be created with the 4-byte
     51   //              big-endian array
     52   static const void IntToBigEndianBytes(uint32_t value,
     53                                         uint8_t*& bytes);
     54 
     55   // Converts a big-endian array of bytes to an unsigned-integer. The given byte
     56   // array must contain 4 bytes.
     57   // @param bytes a big-endian array of bytes
     58   // @return the unsigned integer representation of the given byte array
     59   static const uint32_t BigEndianBytesToInt(const uint8_t* bytes);
     60 
     61   // Generates a random array of bytes with the given length. NULL is returned
     62   // if a random number could not be generated. The returned array must be freed
     63   // using: delete[] bytes.
     64   // @param length the number of random bytes to generate
     65   // @return an array of random bytes of the given length
     66   static uint8_t* GenerateRandomBytes(size_t length);
     67 };
     68 
     69 }  // namespace util
     70 }  // namespace polo
     71 
     72 #endif  // POLO_UTIL_POLOUTIL_H_
     73