資料加密與安全專題《mbedtls工具篇,實用教程2@hello命令原始碼分析》
阿新 • • 發佈:2021-01-25
上一篇中提及mbedtls工具安裝後,可以使用hello命令進行驗證是否已安裝成功,那麼hello命令來自哪裡,如何增加一條自定義的命令
原始碼路徑:mbedtls/programs/hash
目錄結構:
可以看到原始碼hello.c原始檔,檢視原始碼:
/* * Classic "Hello, world" demonstration program * * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * This file is part of mbed TLS (https://tls.mbed.org) */ #if !defined(MBEDTLS_CONFIG_FILE) #include "mbedtls/config.h" #else #include MBEDTLS_CONFIG_FILE #endif #if defined(MBEDTLS_PLATFORM_C) #include "mbedtls/platform.h" #else #include <stdlib.h> #include <stdio.h> #define mbedtls_printf printf #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE #endif #if defined(MBEDTLS_MD5_C) #include "mbedtls/md5.h" #endif #if !defined(MBEDTLS_MD5_C) int main( void ) { mbedtls_printf("MBEDTLS_MD5_C not defined.\n"); return( 0 ); } #else int main( void ) { int i, ret; unsigned char digest[16]; char str[] = "Hello, world!"; mbedtls_printf( "\n MD5('%s') = ", str ); if( ( ret = mbedtls_md5_ret( (unsigned char *) str, 13, digest ) ) != 0 ) return( MBEDTLS_EXIT_FAILURE ); for( i = 0; i < 16; i++ ) mbedtls_printf( "%02x", digest[i] ); mbedtls_printf( "\n\n" ); #if defined(_WIN32) mbedtls_printf( " Press Enter to exit this program.\n" ); fflush( stdout ); getchar(); #endif return( MBEDTLS_EXIT_SUCCESS ); } #endif /* MBEDTLS_MD5_C */
這麼看來Ubuntu終端中的hello命令,就是由hello.c原始檔生成,並拷貝到/usr/local/bin/資料夾中,於是進一步驗證:
再看下CMakeLists.txt檔案內容來驗證:
add_executable(hello hello.c) target_link_libraries(hello mbedtls) add_executable(generic_sum generic_sum.c) target_link_libraries(generic_sum mbedtls) install(TARGETS hello generic_sum DESTINATION "bin" PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
通過以上分析,得出hello命令的由來,按照hello命令的編寫方法,可以嘗試增加自定義的指令了,動起手來吧!