• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

ink_code.cc

Go to the documentation of this file.
00001 /** @file
00002 
00003   A brief file description
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
00022  */
00023 
00024 #include <string.h>
00025 #include <stdio.h>
00026 #include "ink_code.h"
00027 #include "INK_MD5.h"
00028 #include "ink_assert.h"
00029 #include "INK_MD5.h"
00030 
00031 ats::CryptoHash const ats::CRYPTO_HASH_ZERO; // default constructed is correct.
00032 
00033 MD5Context::MD5Context() {
00034   MD5_Init(&_ctx);
00035 }
00036 
00037 bool
00038 MD5Context::update(void const* data, int length) {
00039   return 0 != MD5_Update(&_ctx, data, length);
00040 }
00041 
00042 bool
00043 MD5Context::finalize(CryptoHash& hash) {
00044   return 0 != MD5_Final(hash.u8, &_ctx);
00045 }
00046 
00047 /**
00048   @brief Wrapper around MD5_Init
00049 */
00050 int
00051 ink_code_incr_md5_init(INK_DIGEST_CTX * context) {
00052   return MD5_Init(context);
00053 }
00054 
00055 /**
00056   @brief Wrapper around MD5_Update
00057 */
00058 int
00059 ink_code_incr_md5_update(INK_DIGEST_CTX * context, const char *input, int input_length) {
00060   return MD5_Update(context, input, input_length);
00061 }
00062 
00063 /**
00064   @brief Wrapper around MD5_Final
00065 */
00066 int
00067 ink_code_incr_md5_final(char *sixteen_byte_hash_pointer, INK_DIGEST_CTX * context) {
00068   return MD5_Final((unsigned char*)sixteen_byte_hash_pointer, context);
00069 }
00070 
00071 /**
00072   @brief Helper that will init, update, and create a final MD5
00073 
00074   @return always returns 0, maybe some error checking should be done
00075 */
00076 int
00077 ink_code_md5(unsigned char const* input, int input_length, unsigned char *sixteen_byte_hash_pointer)
00078 {
00079   MD5_CTX context;
00080 
00081   MD5_Init(&context);
00082   MD5_Update(&context, input, input_length);
00083   MD5_Final(sixteen_byte_hash_pointer, &context);
00084 
00085   return (0);
00086 }
00087 
00088 /**
00089   @brief Converts a MD5 to a null-terminated string
00090 
00091   Externalizes an INK_MD5 as a null-terminated string into the first argument.
00092   Side Effects: none
00093   Reentrancy:     n/a.
00094   Thread Safety:  safe.
00095   Mem Management: stomps the passed dest char*.
00096 
00097   @return returns the passed destination string ptr.
00098 */
00099 /* reentrant version */
00100 char *
00101 ink_code_md5_stringify(char *dest33, const size_t destSize, const char *md5)
00102 {
00103   ink_assert(destSize >= 33);
00104 
00105   int i;
00106   for (i = 0; i < 16; i++) {
00107     // we check the size of the destination buffer above
00108     // coverity[secure_coding]
00109     sprintf(&(dest33[i * 2]), "%02X", md5[i]);
00110   }
00111   ink_assert(dest33[32] == '\0');
00112   return (dest33);
00113 }                               /* End ink_code_stringify_md5(const char *md5) */
00114 
00115 /**
00116   @brief Converts a MD5 to a null-terminated string
00117 
00118   Externalizes an INK_MD5 as a null-terminated string into the first argument.
00119   Does so without intenal procedure calls.
00120   Side Effects: none.
00121   Reentrancy:     n/a.
00122   Thread Safety:  safe.
00123   Mem Management: stomps the passed dest char*.
00124 
00125   @return returns the passed destination string ptr.
00126 */
00127 /* reentrant version */
00128 char *
00129 ink_code_to_hex_str(char *dest33, uint8_t const* hash)
00130 {
00131   int i;
00132   char *d;
00133 
00134   static char hex_digits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
00135 
00136   d = dest33;
00137   for (i = 0; i < 16; i += 4) {
00138     *(d + 0) = hex_digits[hash[i + 0] >> 4];
00139     *(d + 1) = hex_digits[hash[i + 0] & 15];
00140     *(d + 2) = hex_digits[hash[i + 1] >> 4];
00141     *(d + 3) = hex_digits[hash[i + 1] & 15];
00142     *(d + 4) = hex_digits[hash[i + 2] >> 4];
00143     *(d + 5) = hex_digits[hash[i + 2] & 15];
00144     *(d + 6) = hex_digits[hash[i + 3] >> 4];
00145     *(d + 7) = hex_digits[hash[i + 3] & 15];
00146     d += 8;
00147   }
00148   *d = '\0';
00149   return (dest33);
00150 }
00151 

Generated by  doxygen 1.7.1