int32_t CryptoAuth_addUser(String* password, uint8_t authType, String* user, struct CryptoAuth* ca) { struct CryptoAuth_pvt* context = Identity_check((struct CryptoAuth_pvt*) ca); if (authType != 1) { return CryptoAuth_addUser_INVALID_AUTHTYPE; } if (context->passwordCount == context->passwordCapacity) { // TODO(cjd): realloc password space and increase buffer. return CryptoAuth_addUser_OUT_OF_SPACE; } struct CryptoAuth_Auth a; hashPassword_sha256(&a, password); for (uint32_t i = 0; i < context->passwordCount; i++) { if (!Bits_memcmp(a.secret, context->passwords[i].secret, 32) || String_equals(user, context->passwords[i].user)) { return CryptoAuth_addUser_DUPLICATE; } } a.user = String_new(user->bytes, context->allocator); Bits_memcpyConst(&context->passwords[context->passwordCount], &a, sizeof(struct CryptoAuth_Auth)); context->passwordCount++; return 0; }
int32_t CryptoAuth_addUser(String* password, uint8_t authType, void* user, struct CryptoAuth* ca) { struct CryptoAuth_pvt* context = (struct CryptoAuth_pvt*) ca; if (authType != 1) { return CryptoAuth_addUser_INVALID_AUTHTYPE; } if (context->passwordCount == context->passwordCapacity) { // TODO: realloc password space and increase buffer. return CryptoAuth_addUser_OUT_OF_SPACE; } struct CryptoAuth_Auth a; hashPassword_sha256(&a, password); for (uint32_t i = 0; i < context->passwordCount; i++) { if (!memcmp(a.secret, context->passwords[i].secret, 32)) { return CryptoAuth_addUser_DUPLICATE; } } a.user = user; Bits_memcpyConst(&context->passwords[context->passwordCount], &a, sizeof(struct CryptoAuth_Auth)); context->passwordCount++; return 0; }
static inline uint8_t* hashPassword(struct Auth* auth, const String* password, const uint8_t authType) { switch (authType) { case 1: hashPassword_sha256(auth, password); }; return auth->secret; }
static inline uint8_t* hashPassword(struct CryptoAuth_Auth* auth, const String* password, const uint8_t authType) { switch (authType) { case 1: hashPassword_sha256(auth, password); break; default: Assert_true(!"Unsupported auth type."); }; return auth->secret; }