From c0107fb90ec39854cfc7723a234ab3753fd3419e Mon Sep 17 00:00:00 2001 From: Maarten Billemont Date: Sat, 24 Mar 2018 12:46:43 -0400 Subject: [PATCH] Move mpw_identicon into mpw-algorithm.h & define colors. --- core/c/mpw-algorithm.c | 37 +++++++++++++++++++++++++++++++++++++ core/c/mpw-algorithm.h | 3 +++ core/c/mpw-types.c | 37 ------------------------------------- core/c/mpw-types.h | 19 +++++++++++++++---- 4 files changed, 55 insertions(+), 41 deletions(-) diff --git a/core/c/mpw-algorithm.c b/core/c/mpw-algorithm.c index f8c46dc7..b089eac8 100644 --- a/core/c/mpw-algorithm.c +++ b/core/c/mpw-algorithm.c @@ -187,3 +187,40 @@ const char *mpw_siteState( return NULL; } } + +MPIdenticon mpw_identicon(const char *fullName, const char *masterPassword) { + + const char *leftArm[] = { "╔", "╚", "╰", "═" }; + const char *rightArm[] = { "╗", "╝", "╯", "═" }; + const char *body[] = { "█", "░", "▒", "▓", "☺", "☻" }; + const char *accessory[] = { + "◈", "◎", "◐", "◑", "◒", "◓", "☀", "☁", "☂", "☃", "", "★", "☆", "☎", "☏", "⎈", "⌂", "☘", "☢", "☣", + "☕", "⌚", "⌛", "⏰", "⚡", "⛄", "⛅", "☔", "♔", "♕", "♖", "♗", "♘", "♙", "♚", "♛", "♜", "♝", "♞", "♟", + "♨", "♩", "♪", "♫", "⚐", "⚑", "⚔", "⚖", "⚙", "⚠", "⌘", "⏎", "✄", "✆", "✈", "✉", "✌" + }; + + const uint8_t *identiconSeed = NULL; + if (fullName && strlen( fullName ) && masterPassword && strlen( masterPassword )) + identiconSeed = mpw_hash_hmac_sha256( + (const uint8_t *)masterPassword, strlen( masterPassword ), + (const uint8_t *)fullName, strlen( fullName ) ); + if (!identiconSeed) + return (MPIdenticon){ + .leftArm = "", + .body = "", + .rightArm = "", + .accessory = "", + .color=0, + }; + + MPIdenticon identicon = { + .leftArm = leftArm[identiconSeed[0] % (sizeof( leftArm ) / sizeof( leftArm[0] ))], + .body = body[identiconSeed[1] % (sizeof( body ) / sizeof( body[0] ))], + .rightArm = rightArm[identiconSeed[2] % (sizeof( rightArm ) / sizeof( rightArm[0] ))], + .accessory = accessory[identiconSeed[3] % (sizeof( accessory ) / sizeof( accessory[0] ))], + .color = (uint8_t)(identiconSeed[4] % (MPIdenticonColorLast - MPIdenticonColorFirst + 1) + MPIdenticonColorFirst), + }; + mpw_free( &identiconSeed, 32 ); + + return identicon; +} diff --git a/core/c/mpw-algorithm.h b/core/c/mpw-algorithm.h index 7840c387..3cf8d702 100644 --- a/core/c/mpw-algorithm.h +++ b/core/c/mpw-algorithm.h @@ -66,4 +66,7 @@ const char *mpw_siteState( const MPResultType resultType, const char *resultParam, const MPAlgorithmVersion algorithmVersion); +/** @return A fingerprint for a user. */ +MPIdenticon mpw_identicon(const char *fullName, const char *masterPassword); + #endif // _MPW_ALGORITHM_H diff --git a/core/c/mpw-types.c b/core/c/mpw-types.c index 93da1aba..ad74b057 100644 --- a/core/c/mpw-types.c +++ b/core/c/mpw-types.c @@ -262,40 +262,3 @@ const char mpw_characterFromClass(char characterClass, uint8_t seedByte) { return classCharacters[seedByte % strlen( classCharacters )]; } - -MPIdenticon mpw_identicon(const char *fullName, const char *masterPassword) { - - const char *leftArm[] = { "╔", "╚", "╰", "═" }; - const char *rightArm[] = { "╗", "╝", "╯", "═" }; - const char *body[] = { "█", "░", "▒", "▓", "☺", "☻" }; - const char *accessory[] = { - "◈", "◎", "◐", "◑", "◒", "◓", "☀", "☁", "☂", "☃", "", "★", "☆", "☎", "☏", "⎈", "⌂", "☘", "☢", "☣", - "☕", "⌚", "⌛", "⏰", "⚡", "⛄", "⛅", "☔", "♔", "♕", "♖", "♗", "♘", "♙", "♚", "♛", "♜", "♝", "♞", "♟", - "♨", "♩", "♪", "♫", "⚐", "⚑", "⚔", "⚖", "⚙", "⚠", "⌘", "⏎", "✄", "✆", "✈", "✉", "✌" - }; - - const uint8_t *identiconSeed = NULL; - if (fullName && strlen( fullName ) && masterPassword && strlen( masterPassword )) - identiconSeed = mpw_hash_hmac_sha256( - (const uint8_t *)masterPassword, strlen( masterPassword ), - (const uint8_t *)fullName, strlen( fullName ) ); - if (!identiconSeed) - return (MPIdenticon){ - .leftArm = "", - .body = "", - .rightArm = "", - .accessory = "", - .color=0, - }; - - MPIdenticon identicon = { - .leftArm = leftArm[identiconSeed[0] % (sizeof( leftArm ) / sizeof( leftArm[0] ))], - .body = body[identiconSeed[1] % (sizeof( body ) / sizeof( body[0] ))], - .rightArm = rightArm[identiconSeed[2] % (sizeof( rightArm ) / sizeof( rightArm[0] ))], - .accessory = accessory[identiconSeed[3] % (sizeof( accessory ) / sizeof( accessory[0] ))], - .color = (uint8_t)(identiconSeed[4] % 7 + 1), - }; - mpw_free( &identiconSeed, 32 ); - - return identicon; -} diff --git a/core/c/mpw-types.h b/core/c/mpw-types.h index 347f625b..fa56c819 100644 --- a/core/c/mpw-types.h +++ b/core/c/mpw-types.h @@ -111,12 +111,26 @@ typedef mpw_enum ( uint32_t, MPCounterValue ) { MPCounterValueLast = UINT32_MAX, }; +/** These colours are compatible with the original ANSI SGR. */ +typedef mpw_enum( uint8_t, MPIdenticonColor ) { + MPIdenticonColorRed = 1, + MPIdenticonColorGreen, + MPIdenticonColorYellow, + MPIdenticonColorBlue, + MPIdenticonColorMagenta, + MPIdenticonColorCyan, + MPIdenticonColorWhite, + + MPIdenticonColorFirst = MPIdenticonColorRed, + MPIdenticonColorLast = MPIdenticonColorWhite, +}; + typedef struct { const char *leftArm; const char *body; const char *rightArm; const char *accessory; - uint8_t color; + MPIdenticonColor color; } MPIdenticon; //// Type utilities. @@ -165,7 +179,4 @@ const char *mpw_charactersInClass(char characterClass); */ const char mpw_characterFromClass(char characterClass, uint8_t seedByte); -/** @return A fingerprint for a user. */ -MPIdenticon mpw_identicon(const char *fullName, const char *masterPassword); - #endif // _MPW_TYPES_H