这是小沃最近遇到的一个项目,由于百度云的所有接口都需要通过hmac sha256计算,所以小沃在网上找到了相关代码可以在单片机中实现,现在就分享给大家。
SHA256.h#ifndef SHA256_H
#define SHA256_H
#include
typedef struct {
uint8_t hash[32];
uint32_t buffer[16];
uint32_t state[8];
uint8_t length[8];
} sha256;
extern void sha256_get(uint8_t hash[32],
const uint8_t *message,
int length);
extern void hmac_sha256_get(uint8_t digest[32],
uint8_t *message, int message_length,
uint8_t *key, int key_length);
#endif
SHA256.c/*
* Copyright 2006 Apple Computer, Inc. All rights reserved.
*
* iTunes U Sample Code License
* IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc. ("Apple")
* in consideration of your agreement to the following terms, and your use,
* installation, modification or distribution of this Apple software constitutes
* acceptance of these terms. If you do not agree with these terms, please do not use,
* install, modify or distribute this Apple software.
*
* In consideration of your agreement to abide by the following terms and subject to
* these terms, Apple grants you a personal, non-exclusive, non-transferable license,
* under Apple's copyrights in this original Apple software (the "Apple Software"):
*
* (a) to internally use, reproduce, modify and internally distribute the Apple
* Software, with or without modifications, in source and binary forms, within your
* educational organization or internal campus network for the sole purpose of
* integrating Apple's iTunes U software with your internal campus network systems; and
*
* (b) to redistribute the Apple Software to other universities or educational
* organizations, with or without modifications, in source and binary forms, for the
* sole purpose of integrating Apple's iTunes U software with their internal campus
* network systems; provided that the following conditions are met:
*
* - If you redistribute the Apple Software in its entirety and without
* modifications, you must retain the above copyright notice, this entire license
* and the disclaimer provisions in all such redistributions of the Apple Software.
* - If you modify and redistribute the Apple Software, you must indicate that you
* have made changes to the Apple Software, and you must retain the above
* copyright notice, this entire license and the disclaimer provisions in all
* such redistributions of the Apple Software and/or derivatives thereof created
* by you.
* - Neither the name, trademarks, service marks or logos of Apple may be used to
* endorse or promote products derived from the Apple Software without specific
* prior written permission from Apple.
*
* Except as expressly stated above, no other rights or licenses, express or implied,
* ar