Skip to content

Add MD5 API and example #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions examples/MD5/MD5.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
ArduinoBearSSL MD5

This sketch demonstrates how to create a MD5 hash
for an input string.

This example code is in the public domain.
*/

#include <ArduinoBearSSL.h>

void setup() {
Serial.begin(9600);
while (!Serial);

// expect 9e107d9d372bb6826bd81d3542a419d6
printMD5("The quick brown fox jumps over the lazy dog");

// expect 80070713463e7749b90c2dc24911e275
printHMACMD5("key", "The quick brown fox jumps over the lazy dog");
}

void loop() {
}

void printMD5(const char* str) {
Serial.print("MD5 of '");
Serial.print(str);
Serial.print("' is 0x");

MD5.beginHash();
MD5.print(str);
MD5.endHash();

printResult();
}

void printHMACMD5(const char* secret, const char* str) {
Serial.print("HMAC-MD5 of '");
Serial.print(str);
Serial.print("' with secret '");
Serial.print(secret);
Serial.print("' is 0x");

MD5.beginHmac(secret);
MD5.print(str);
MD5.endHmac();

printResult();
}

void printResult()
{
while (MD5.available()) {
byte b = MD5.read();

if (b < 16) {
Serial.print("0");
}

Serial.print(b, HEX);
}
Serial.println();
}
1 change: 1 addition & 0 deletions src/ArduinoBearSSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "BearSSLClient.h"
#include "SHA1.h"
#include "SHA256.h"
#include "MD5.h"

class ArduinoBearSSLClass {
public:
Expand Down
57 changes: 57 additions & 0 deletions src/MD5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2019 Arduino SA. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include "MD5.h"

MD5Class::MD5Class() :
SHAClass(MD5_BLOCK_SIZE, MD5_DIGEST_SIZE)
{
}

MD5Class::~MD5Class()
{
}

int MD5Class::begin()
{
br_md5_init(&_ctx);

return 1;
}

int MD5Class::update(const uint8_t *buffer, size_t size)
{
br_md5_update(&_ctx, buffer, size);

return 1;
}

int MD5Class::end(uint8_t *digest)
{
br_md5_out(&_ctx, digest);

return 1;
}

MD5Class MD5;
52 changes: 52 additions & 0 deletions src/MD5.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2019 Arduino SA. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#ifndef MD5_H
#define MD5_H

#include <bearssl/bearssl_hash.h>

#include "SHA.h"

#define MD5_BLOCK_SIZE 64
#define MD5_DIGEST_SIZE 16

class MD5Class: public SHAClass {

public:
MD5Class();
virtual ~MD5Class();

protected:
virtual int begin();
virtual int update(const uint8_t *buffer, size_t size);
virtual int end(uint8_t *digest);

private:
br_md5_context _ctx;
};

extern MD5Class MD5;

#endif