关于数据加密问题(3)

关于数据加密问题

本文介绍下sha1,256加密方式
同样,先看下各自的头文件
sha1.h

/**
 * \file sha1.h
 *
 * \brief SHA-1 cryptographic hash function
 *
 *  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)
 */
#ifndef MBEDTLS_SHA1_H
#define MBEDTLS_SHA1_H

#if !defined(MBEDTLS_CONFIG_FILE)
#include "config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif

#include <stddef.h>
#include <stdint.h>

#if !defined(MBEDTLS_SHA1_ALT)
// Regular implementation
//

#ifdef __cplusplus
extern "C" {
#endif

/**
 * \brief          SHA-1 context structure
 */
typedef struct
{
    uint32_t total[2];          /*!< number of bytes processed  */
    uint32_t state[5];          /*!< intermediate digest state  */
    unsigned char buffer[64];   /*!< data block being processed */
}
mbedtls_sha1_context;

/**
 * \brief          Initialize SHA-1 context
 *
 * \param ctx      SHA-1 context to be initialized
 */
void mbedtls_sha1_init( mbedtls_sha1_context *ctx );

/**
 * \brief          Clear SHA-1 context
 *
 * \param ctx      SHA-1 context to be cleared
 */
void mbedtls_sha1_free( mbedtls_sha1_context *ctx );

/**
 * \brief          Clone (the state of) a SHA-1 context
 *
 * \param dst      The destination context
 * \param src      The context to be cloned
 */
void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
                         const mbedtls_sha1_context *src );

/**
 * \brief          SHA-1 context setup
 *
 * \param ctx      context to be initialized
 */
void mbedtls_sha1_starts( mbedtls_sha1_context *ctx );

/**
 * \brief          SHA-1 process buffer
 *
 * \param ctx      SHA-1 context
 * \param input    buffer holding the  data
 * \param ilen     length of the input data
 */
void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen );

/**
 * \brief          SHA-1 final digest
 *
 * \param ctx      SHA-1 context
 * \param output   SHA-1 checksum result
 */
void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] );

/* Internal use */
void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] );

#ifdef __cplusplus
}
#endif

#else  /* MBEDTLS_SHA1_ALT */
#include "sha1_alt.h"
#endif /* MBEDTLS_SHA1_ALT */

#ifdef __cplusplus
extern "C" {
#endif

/**
 * \brief          Output = SHA-1( input buffer )
 *
 * \param input    buffer holding the  data
 * \param ilen     length of the input data
 * \param output   SHA-1 checksum result
 */
void mbedtls_sha1( const unsigned char *input, size_t ilen, unsigned char output[20] );//直接进行sha1加密api

/**
 * \brief          Checkup routine
 *
 * \return         0 if successful, or 1 if the test failed
 */
int mbedtls_sha1_self_test( int verbose );

#ifdef __cplusplus
}
#endif

#endif /* mbedtls_sha1.h */

sha256.h

/**
 * \file sha256.h
 *
 * \brief SHA-224 and SHA-256 cryptographic hash function
 *
 *  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)
 */
#ifndef MBEDTLS_SHA256_H
#define MBEDTLS_SHA256_H

#if !defined(MBEDTLS_CONFIG_FILE)
#include "config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif

#include <stddef.h>
#include <stdint.h>

#if !defined(MBEDTLS_SHA256_ALT)
// Regular implementation
//

#ifdef __cplusplus
extern "C" {
#endif

/**
 * \brief          SHA-256 context structure
 */
typedef struct
{
    uint32_t total[2];          /*!< number of bytes processed  */
    uint32_t state[8];          /*!< intermediate digest state  */
    unsigned char buffer[64];   /*!< data block being processed */
    int is224;                  /*!< 0 => SHA-256, else SHA-224 */
}
mbedtls_sha256_context;

/**
 * \brief          Initialize SHA-256 context
 *
 * \param ctx      SHA-256 context to be initialized
 */
void mbedtls_sha256_init( mbedtls_sha256_context *ctx );

/**
 * \brief          Clear SHA-256 context
 *
 * \param ctx      SHA-256 context to be cleared
 */
void mbedtls_sha256_free( mbedtls_sha256_context *ctx );

/**
 * \brief          Clone (the state of) a SHA-256 context
 *
 * \param dst      The destination context
 * \param src      The context to be cloned
 */
void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
                           const mbedtls_sha256_context *src );

/**
 * \brief          SHA-256 context setup
 *
 * \param ctx      context to be initialized
 * \param is224    0 = use SHA256, 1 = use SHA224
 */
void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 );

/**
 * \brief          SHA-256 process buffer
 *
 * \param ctx      SHA-256 context
 * \param input    buffer holding the  data
 * \param ilen     length of the input data
 */
void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input,
                    size_t ilen );

/**
 * \brief          SHA-256 final digest
 *
 * \param ctx      SHA-256 context
 * \param output   SHA-224/256 checksum result
 */
void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32] );

/* Internal use */
void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] );

#ifdef __cplusplus
}
#endif

#else  /* MBEDTLS_SHA256_ALT */
#include "sha256_alt.h"
#endif /* MBEDTLS_SHA256_ALT */

#ifdef __cplusplus
extern "C" {
#endif

/**
 * \brief          Output = SHA-256( input buffer )
 *
 * \param input    buffer holding the  data
 * \param ilen     length of the input data
 * \param output   SHA-224/256 checksum result
 * \param is224    0 = use SHA256, 1 = use SHA224
 */
void mbedtls_sha256( const unsigned char *input, size_t ilen,
           unsigned char output[32], int is224 );

/**
 * \brief          Checkup routine
 *
 * \return         0 if successful, or 1 if the test failed
 */
int mbedtls_sha256_self_test( int verbose );

#ifdef __cplusplus
}
#endif

#endif /* mbedtls_sha256.h */

测试程序

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<mbedtls/sha1.h>
#include<mbedtls/sha256.h>

int main(int argc, const char *argv[])
{
        unsigned char date[64] = "hello world";
        unsigned char outstring_sha1[64];
        unsigned char outstring_sha256[64];

        unsigned char outsha1[64];
        unsigned char outsha256[64];

        memset(outstring_sha1,0,64);
        memset(outstring_sha256,0,64);
        memset(outsha1,0,64);
        memset(outsha256,0,64);
        
        int i;
        int len;
        len = strlen(date);

        //先看sha1编码
        mbedtls_sha1(date,len,outstring_sha1);

        printf("sha1 str:%s\n",outstring_sha1);

        for(i=0;outstring_sha1[i]!='\0';i++)
        {
                snprintf(outsha1+i*2,3,"%02x",outstring_sha1[i]);
        }

        printf("outsha1:%s\n",outsha1);

        //sha256编码
        mbedtls_sha256(date,len,outstring_sha256,0);

        printf("sha256 str:%s\n",outstring_sha256);

        for(i=0;outstring_sha256[i]!='\0';i++)
        {
                snprintf(outsha256+i*2,3,"%02x",outstring_sha256[i]);
        }

        printf("outsha256:%s\n",outsha256);
        return 0;
}

mbedtls_sha1/mbedtls_sha256 《===》mbedtls_sha1_init,mbedtls_sha1_starts,mbedtls_sha1_update,mbedtls_sha1_finish
两者等价,从md5中仿照过来,没有做单独测试,最后验证。

编译

gcc sha_test.c -o sha1_test -L/usr/local/lib -lmbedcrypto

编译成功
在这里插入图片描述
运行
在这里插入图片描述
在线工具加密:点击这里
sha1
在这里插入图片描述
sha256
在这里插入图片描述

注意:此处验证下mbedtls_sha1/mbedtls_sha256 《===》mbedtls_sha1_init,mbedtls_sha1_starts,mbedtls_sha1_update,mbedtls_sha1_finish

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<mbedtls/sha1.h>
#include<mbedtls/sha256.h>

void sha1_encode(unsigned char * date,unsigned char * out)
{
        mbedtls_sha1_context sha1_state;

        mbedtls_sha1_init(&sha1_state);

        mbedtls_sha1_starts(&sha1_state);

        mbedtls_sha1_update(&sha1_state,date,strlen(date));

        mbedtls_sha1_finish(&sha1_state,out);

}



int main(int argc, const char *argv[])
{
        unsigned char date[64] = "hello world";
        unsigned char outstring_sha1[64];
        unsigned char outstring_sha256[64];

        unsigned char outsha1[64];
        unsigned char outsha256[64];

        //使用组合的api接口进行sha1编码
        unsigned char outstring[64];
        unsigned char outstr[64];
        int i;
        int len;
        len = strlen(date);

        memset(outstring_sha1,0,64);
        memset(outstring_sha256,0,64);
        memset(outsha1,0,64);
        memset(outsha256,0,64);
        memset(outstring,0,64);
        memset(outstr,0,64);

        //先看sha1编码
        mbedtls_sha1(date,len,outstring_sha1);

        printf("sha1 str:%s\n",outstring_sha1);

        for(i=0;outstring_sha1[i]!='\0';i++)
        {
                snprintf(outsha1+i*2,3,"%02x",outstring_sha1[i]);
        }

        printf("outsha1:%s\n",outsha1);
#if 0
        //sha256编码
        mbedtls_sha256(date,len,outstring_sha256,0);

        printf("sha256 str:%s\n",outstring_sha256);

        for(i=0;outstring_sha256[i]!='\0';i++)
        {
                snprintf(outsha256+i*2,3,"%02x",outstring_sha256[i]);
        }

        printf("outsha256:%s\n",outsha256);
#endif
#if 1
        sha1_encode(date,outstring);

        for(i=0;outstring[i] != '\0';i++)
        {
                snprintf(outstr+i*2,3,"%02x",outstring[i]);
        }
        printf("comb api outstr:%s\n",outstr);
#endif
        return 0;
}

编译运行结果
在这里插入图片描述
所以两种是等价的。

发布了73 篇原创文章 · 获赞 6 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_38240926/article/details/100743900