QByteArray Sha256(const QByteArray& data) { SHA256_CTX context; SHA256_Init(&context); SHA256_Update(&context, reinterpret_cast<const u_int8_t*>(data.constData()), data.length()); QByteArray ret(SHA256_DIGEST_LENGTH, '\0'); SHA256_Final(reinterpret_cast<u_int8_t*>(ret.data()), &context); return ret; }
static unsigned sha256_finish(pgp_hash_t *hash, uint8_t *out) { SHA256_Final(out, hash->data); if (pgp_get_debug_level(__FILE__)) { hexdump(stderr, "sha1_finish", out, SHA256_DIGEST_LENGTH); } free(hash->data); hash->data = NULL; return SHA256_DIGEST_LENGTH; }
//------------------------------------------------------------------------------ // helper function to compute SHA256: static std::vector<unsigned char> sha256(const std::string& data) { std::vector<unsigned char> digest(SHA256_DIGEST_LENGTH); SHA256_CTX ctx; SHA256_Init(&ctx); SHA256_Update(&ctx, data.c_str(), data.length()); SHA256_Final(digest.data(), &ctx); return digest; }
/* *Register */ int register_user(packet *in_pkt, int fd) { int i = 0; char *args[16]; char cpy[BUFFERSIZE]; char *tmp = cpy; strcpy(tmp, in_pkt->buf); args[i] = strsep(&tmp, " \t"); while ((i < sizeof(args) - 1) && (args[i] != '\0')) { args[++i] = strsep(&tmp, " \t"); } // Check there are enough arguements to safely inspect them if (i > 3) { // Ensure requested username is valid if (!validUsername(args[1], fd)) { return 0; } // Check if the requested username is unique if(strcmp(get_real_name(®istered_users_list, args[1], registered_users_mutex), "ERROR") !=0 || \ !(strcmp(SERVER_NAME, args[1])) || \ strcmp(args[2], args[3]) != 0) { sendError("Username unavailable.", fd); return 0; } // Ensure password requested is valid if (!validPassword(args[2], args[3], fd)) { return 0; } // Allocate memory space for new user node, populate node with new user data User *user = (User *)malloc(sizeof(User)); strcpy(user->username, args[1]); strcpy(user->real_name, args[1]); // Hash password SHA256_CTX sha256; SHA256_Init(&sha256); SHA256_Update(&sha256, args[2], strlen(args[2])); SHA256_Final(user->password, &sha256); user->sock = fd; user->next = NULL; // Insert user as registered user, write new user data to file insertUser(®istered_users_list, user, registered_users_mutex); writeUserFile(®istered_users_list, USERS_FILE, registered_users_mutex); // Reform packet as valid login, pass new user data to login memset(&in_pkt->buf, 0, sizeof(in_pkt->buf)); sprintf(in_pkt->buf, "/login %s %s", args[1], args[2]); return login(in_pkt, fd); } // There were not enough arguements received to correctly read them else { printf("%s --- %sError:%s Malformed reg packet received from %s on %d, ignoring.\n", \ WHITE, RED, NORMAL, args[1], fd); } return 0; }
static gboolean hash_stream(FILE *stream, uint8_t *hash, GError **error) { uint8_t buf[1024]; SHA256_CTX ctx; gboolean ret = TRUE; if (!SHA256_Init(&ctx)) { g_set_error(error, CRYPT_ERROR, CRYPT_ERROR_INIT, "Failed to initialise an SHA256 context"); ret = FALSE; } else { int final_result; while (TRUE) { size_t got = fread(buf, 1, sizeof(buf), stream); if (!SHA256_Update(&ctx, buf, got)) { g_set_error(error, CRYPT_ERROR, CRYPT_ERROR_HASH, "Error calculating the SHA256 hash"); ret = FALSE; } else if (got != sizeof(buf)) { if (ferror(stream)) { g_set_error_literal(error, G_FILE_ERROR, g_file_error_from_errno(errno), strerror(errno)); ret = FALSE; } break; } } /* I think we have to call this regardless of whether there was * an error in order to clear up the resources */ final_result = SHA256_Final((unsigned char *)hash, &ctx); if (!final_result && ret) { g_set_error(error, CRYPT_ERROR, CRYPT_ERROR_HASH, "Error calculating the SHA256 hash"); ret = FALSE; } } return ret; }
static void hash_password_salted (const char *passwd, char *hashed_passwd) { unsigned char sha[SHA256_DIGEST_LENGTH]; SHA256_CTX s; SHA256_Init (&s); SHA256_Update (&s, passwd, strlen(passwd)); SHA256_Update (&s, salt, sizeof(salt)); SHA256_Final (sha, &s); rawdata_to_hex (sha, hashed_passwd, SHA256_DIGEST_LENGTH); }
/* * Applies sha256 using the given context and input/length, and returns * a double in the range [0...1[ based on the hash. */ static double vdi_random_sha(const char *input, ssize_t len) { struct SHA256Context ctx; uint8_t sign[SHA256_LEN]; AN(input); SHA256_Init(&ctx); SHA256_Update(&ctx, input, len); SHA256_Final(sign, &ctx); return (vle32dec(sign) / exp2(32)); }
static int scryptdec_setup(const uint8_t header[96], uint8_t dk[64], const uint8_t * passwd, size_t passwdlen, size_t maxmem, double maxmemfrac, double maxtime, int verbose) { uint8_t salt[32]; uint8_t hbuf[32]; int logN; uint32_t r; uint32_t p; uint64_t N; SHA256_CTX ctx; uint8_t * key_hmac = &dk[32]; HMAC_SHA256_CTX hctx; int rc; /* Parse N, r, p, salt. */ logN = header[7]; r = be32dec(&header[8]); p = be32dec(&header[12]); memcpy(salt, &header[16], 32); /* Verify header checksum. */ SHA256_Init(&ctx); SHA256_Update(&ctx, header, 48); SHA256_Final(hbuf, &ctx); if (memcmp(&header[48], hbuf, 16)) return (7); /* * Check whether the provided parameters are valid and whether the * key derivation function can be computed within the allowed memory * and CPU time. */ if ((rc = checkparams(maxmem, maxmemfrac, maxtime, logN, r, p, verbose)) != 0) return (rc); /* Compute the derived keys. */ N = (uint64_t)(1) << logN; if (crypto_scrypt(passwd, passwdlen, salt, 32, N, r, p, dk, 64)) return (3); /* Check header signature (i.e., verify password). */ HMAC_SHA256_Init(&hctx, key_hmac, 32); HMAC_SHA256_Update(&hctx, header, 64); HMAC_SHA256_Final(hbuf, &hctx); if (memcmp(hbuf, &header[64], 32)) return (11); /* Success! */ return (0); }
void sha256_str(const char *string, char out[SHA256_DIGEST_LENGTH * 2 + 1]) { unsigned char hash[SHA256_DIGEST_LENGTH]; SHA256_CTX sha256; SHA256_Init(&sha256); SHA256_Update(&sha256, string, strlen(string)); SHA256_Final(hash, &sha256); sha256_hash(hash, out); }
gchar *g_checksum_get_string(GChecksum *ctx) { static gchar hexdigest[SHA256_DIGEST_LENGTH*2+1]; guchar digest[SHA256_DIGEST_LENGTH]; SHA256_Final(digest, ctx); for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) { hexdigest[i*2] = _tohex[(digest[i] & 0xf0) >> 4]; hexdigest[i*2+1] = _tohex[(digest[i] & 0x0f)]; } hexdigest[SHA256_DIGEST_LENGTH*2] = '\0'; return hexdigest; }
int hash_str(unsigned char *output, const char *input, int length) { SHA256_CTX sha256; if (SHA256_Init(&sha256) != 1) return 0; if (SHA256_Update(&sha256, input, length) != 1) return 0; if (SHA256_Final(output, &sha256) != 1) return 0; return 1; }
char* SHA256FromHash(ArrayOfHash *array) { unsigned char* hash = malloc(SHA256_DIGEST_LENGTH); SHA256_CTX sha256; SHA256_Init(&sha256); int i; for(i = 0; i < array->size; i++) { SHA256_Update(&sha256, array->arrayOfHash[i], array->sizeBlock); } SHA256_Final(hash, &sha256); return hash; }
void TLSTicketKeyManager::makeUniqueKeys(unsigned char* parentKey, size_t keyLen, unsigned char* salt, unsigned char* output) { SHA256_CTX hash_ctx; SHA256_Init(&hash_ctx); SHA256_Update(&hash_ctx, parentKey, keyLen); SHA256_Update(&hash_ctx, salt, kTLSTicketKeySaltLen); SHA256_Final(output, &hash_ctx); }
static void crypt_all(int count) { int index = 0; #ifdef _OPENMP #pragma omp parallel for for (index = 0; index < count; index++) #endif { SHA256_CTX ctx; int i; SHA256_Init(&ctx); SHA256_Update(&ctx, saved_key[index], strlen(saved_key[index])); SHA256_Update(&ctx, cur_salt->salt, 32); SHA256_Final((unsigned char*)crypt_out[index], &ctx); for(i = 0; i <= cur_salt->iterations; i++) { SHA256_Init(&ctx); SHA256_Update(&ctx, (unsigned char*)crypt_out[index], 32); SHA256_Final((unsigned char*)crypt_out[index], &ctx); } } }
// Hash the password and truncate it. Long salts are good, but what's the point // of storing such large hashes? It's not like an attacker can bruteforce a // valid password without knowing the salt. void hash_password(uint8_t hash[6], const uint8_t salt[32], const char *password) { SHA256_CTX c; char digest[SHA256_DIGEST_LENGTH]; if (SHA256_Init(&c) != 1 || SHA256_Update(&c, password, strlen(password)) != 1 || SHA256_Update(&c, salt, 32) != 1 || SHA256_Final(digest, &c) != 1) { fputs("unable to hash\n", stderr); exit(1); } memcpy(hash, digest, 6); }
unsigned char *SHA256(const unsigned char *d, size_t n, unsigned char *md) { SHA256_CTX c; static unsigned char m[SHA256_DIGEST_LENGTH]; if (md == NULL) md=m; SHA256_Init(&c); SHA256_Update(&c,d,n); SHA256_Final(md,&c); OPENSSL_cleanse(&c,sizeof(c)); return(md); }
void hash(char *data, size_t len, char *buf) { unsigned char hash[SHA256_DIGEST_LENGTH]; SHA256_CTX ctx; SHA256_Init(&ctx); SHA256_Update(&ctx, data, len); SHA256_Final(hash, &ctx); printhash(hash, buf); }
void calc_sha256(unsigned char *data, size_t size, char *sha256sum) { unsigned char hash[SHA256_DIGEST_LENGTH]; SHA256_CTX mdContext; SHA256_Init(&mdContext); SHA256_Update (&mdContext, data, size); SHA256_Final(hash, &mdContext); for(unsigned i=0; i < SHA256_DIGEST_LENGTH; i++) sprintf(&sha256sum[i*2], "%02x", hash[i]); }
static int scryptenc_setup(uint8_t header[96], uint8_t dk[64], const uint8_t * passwd, size_t passwdlen, size_t maxmem, double maxmemfrac, double maxtime) { uint8_t salt[32]; uint8_t hbuf[32]; int logN; uint64_t N; uint32_t r; uint32_t p; SHA256_CTX ctx; uint8_t * key_hmac = &dk[32]; HMAC_SHA256_CTX hctx; int rc; /* Pick values for N, r, p. */ if ((rc = pickparams(maxmem, maxmemfrac, maxtime, &logN, &r, &p)) != 0) return (rc); N = (uint64_t)(1) << logN; /* Get some salt. */ if (crypto_entropy_read(salt, 32)) return (4); /* Generate the derived keys. */ if (crypto_scrypt(passwd, passwdlen, salt, 32, N, r, p, dk, 64)) return (3); /* Construct the file header. */ memcpy(header, "scrypt", 6); header[6] = 0; header[7] = logN; be32enc(&header[8], r); be32enc(&header[12], p); memcpy(&header[16], salt, 32); /* Add header checksum. */ SHA256_Init(&ctx); SHA256_Update(&ctx, header, 48); SHA256_Final(hbuf, &ctx); memcpy(&header[48], hbuf, 16); /* Add header signature (used for verifying password). */ HMAC_SHA256_Init(&hctx, key_hmac, 32); HMAC_SHA256_Update(&hctx, header, 64); HMAC_SHA256_Final(hbuf, &hctx); memcpy(&header[64], hbuf, 32); /* Success! */ return (0); }
void read_bitcoin_transaction(struct space *space, struct bitcoin_transaction *trans, struct file *f, off_t *poff) { size_t i; off_t start = *poff; SHA256_CTX sha256; trans->version = pull_u32(f, poff); trans->input_count = pull_varint(f, poff); trans->input = space_alloc_arr(space, struct bitcoin_transaction_input, trans->input_count); for (i = 0; i < trans->input_count; i++) read_input(space, f, poff, trans->input + i); trans->output_count = pull_varint(f, poff); trans->output = space_alloc_arr(space, struct bitcoin_transaction_output, trans->output_count); for (i = 0; i < trans->output_count; i++) read_output(space, f, poff, trans->output + i); trans->lock_time = pull_u32(f, poff); /* Bitcoin uses double sha (it's not quite known why...) */ SHA256_Init(&sha256); if (likely(f->mmap)) { SHA256_Update(&sha256, f->mmap + start, *poff - start); } else { u8 *buf = tal_arr(NULL, u8, *poff - start); file_read(f, start, *poff - start, buf); SHA256_Update(&sha256, buf, *poff - start); tal_free(buf); } SHA256_Final(trans->sha256, &sha256); SHA256_Init(&sha256); SHA256_Update(&sha256, trans->sha256, sizeof(trans->sha256)); SHA256_Final(trans->sha256, &sha256); trans->len = *poff - start; }
static int hast_sha256_checksum(const unsigned char *data, size_t size, unsigned char *hash, size_t *hsizep) { SHA256_CTX ctx; SHA256_Init(&ctx); SHA256_Update(&ctx, data, size); SHA256_Final(hash, &ctx); *hsizep = SHA256_DIGEST_LENGTH; return (0); }
inline static int pub2hash160(unsigned char *pub_chr) { /* compute hash160 for uncompressed public key */ /* sha256(pub) */ SHA256_Init(sha256_ctx); SHA256_Update(sha256_ctx, pub_chr, 65); SHA256_Final(hash256, sha256_ctx); /* ripemd160(sha256(pub)) */ ripemd160_256(hash256, hash160_uncmp.uc); /* quick and dirty public key compression */ pub_chr[0] = 0x02 | (pub_chr[64] & 0x01); /* compute hash160 for compressed public key */ /* sha256(pub) */ SHA256_Init(sha256_ctx); SHA256_Update(sha256_ctx, pub_chr, 33); SHA256_Final(hash256, sha256_ctx); /* ripemd160(sha256(pub)) */ ripemd160_256(hash256, hash160_compr.uc); return 0; }
void DigestTree::recomputeRoot() { SHA256_CTX sha256; SHA256_Init(&sha256); for (size_t i = 0; i < digestNode_.size(); ++i) SHA256_UpdateHex(&sha256, digestNode_[i]->getDigest()); uint8_t digestRoot[ndn_SHA256_DIGEST_SIZE]; SHA256_Final(&digestRoot[0], &sha256); root_ = toHex(digestRoot, sizeof(digestRoot)); _LOG_DEBUG("update root to: " + root_); }
int sha(void* input, unsigned long length, unsigned char* md) { SHA256_CTX context; if(!SHA256_Init(&context)) return 0; if(!SHA256_Update(&context, (unsigned char*)input, length)) return 0; if(!SHA256_Final(md, &context)) return 0; return 1; }
string sha256(const string &str) { unsigned char hash[SHA256_DIGEST_LENGTH]; SHA256_CTX sha256; SHA256_Init(&sha256); SHA256_Update(&sha256, str.c_str(), str.size()); SHA256_Final(hash, &sha256); stringstream ret; for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) { ret << hex << setw(2) << setfill('0') << static_cast<int>(hash[i]); } return ret.str(); }
std::string Security::ShaProvider::CreateSha256(const std::string& data) const { unsigned char hash[SHA256_DIGEST_LENGTH]; SHA256_CTX sha256; SHA256_Init(&sha256); SHA256_Update(&sha256, data.c_str(), data.size()); SHA256_Final(hash, &sha256); HexUtils hexUtils; std::string shaHash = hexUtils.HexToString(hash, SHA256_DIGEST_LENGTH); return shaHash; }
void hash(char *string, char *result) { unsigned char hash[SHA256_DIGEST_LENGTH]; int i = 0; SHA256_CTX handler; SHA256_Init(&handler); SHA256_Update(&handler, string, strlen(string)); SHA256_Final(hash, &handler); memset(result, '\0', strlen(result)); for (i = 0; i < SHA256_DIGEST_LENGTH; i++) { sprintf(result, "%s%02x", result, hash[i]); }; }
void sha256(unsigned char * data_chunks[], unsigned int data_chunck_length[], unsigned char *digest) { SHA256_CTX ctx; SHA256_Init( &ctx); while(*data_chunks) { SHA256_Update(&ctx, *data_chunks, *data_chunck_length); data_chunks++; data_chunck_length++; } SHA256_Final(digest, &ctx); }
static int hash_final( SRP_HashAlgorithm alg, HashCTX *c, unsigned char *md ) { switch (alg) { case SRP_SHA1 : return SHA1_Final( md, &c->sha ); case SRP_SHA224: return SHA224_Final( md, &c->sha256 ); case SRP_SHA256: return SHA256_Final( md, &c->sha256 ); case SRP_SHA384: return SHA384_Final( md, &c->sha512 ); case SRP_SHA512: return SHA512_Final( md, &c->sha512 ); default: return -1; } }
// Deprecated on OSX... figure out the proper thing with their internal lib later... // Using technique from http://stackoverflow.com/questions/13784434/gcc-use-openssls-sha256-functions inline std::string sha256(std::string str) { unsigned char hbuf[SHA256_DIGEST_LENGTH]; std::stringstream ss; SHA256_CTX h; SHA256_Init(&h); SHA256_Update(&h, str.c_str(), str.size()); SHA256_Final(hbuf, &h); for(int i = 0; i < SHA256_DIGEST_LENGTH; i++) { ss << std::hex << std::setw(2) << std::setfill('0') << (int)hbuf[i]; } return ss.str(); };