static void* sha1_thread(void* arg) { char code[41]; while (1) { struct chunk* c = sync_queue_pop(chunk_queue); if (c == NULL) { sync_queue_term(hash_queue); break; } if (CHECK_CHUNK(c, CHUNK_FILE_START) || CHECK_CHUNK(c, CHUNK_FILE_END)) { sync_queue_push(hash_queue, c); continue; } TIMER_DECLARE(1); TIMER_BEGIN(1); SHA_CTX ctx; SHA_Init(&ctx); SHA_Update(&ctx, c->data, c->size); SHA_Final(c->fp, &ctx); TIMER_END(1, jcr.hash_time); hash2code(c->fp, code); code[40] = 0; VERBOSE("Hash phase: %ldth chunk identified by %s", chunk_num++, code); sync_queue_push(hash_queue, c); } return NULL; }
void cyon_store_current_state(u_int8_t *hash) { SHA_CTX sctx; u_int8_t *buf; struct store_header header; u_int32_t len, blen; memset(&header, 0, sizeof(header)); if (store_passphrase != NULL) header.flags |= STORE_HAS_PASSPHRASE; len = 0; blen = 128 * 1024 * 1024; buf = cyon_malloc(blen); memcpy(buf, &header, sizeof(header)); len += sizeof(header); if (header.flags & STORE_HAS_PASSPHRASE) { memcpy(buf + len, store_passphrase, SHA256_DIGEST_LENGTH); len += SHA256_DIGEST_LENGTH; } store_validation = 1; SHA_Init(&sctx); cyon_store_writenode(-1, rnode, buf, blen, &len, &sctx); if (len > 0) SHA_Update(&sctx, buf, len); cyon_mem_free(buf); SHA_Final(hash, &sctx); store_validation = 0; }
void xr_sha256::start_calculate (u8 const * data, u32 data_size) { SHA_Init (m_sha_ctx); ZeroMemory (m_result, sizeof(m_result)); VERIFY (data_size); m_data_src = data; m_data_size = data_size; }
/** * SHA1WithRSA私钥签名 * * LUA示例: * local codec = require('codec') * local src = 'something' * local pem = [[...]] --私钥PEM字符串 * local bs = codec.rsa_private_sign(src, pem) * local dst = codec.base64_encode(bs) --BASE64签名 */ static int codec_rsa_private_sign(lua_State *L) { size_t len; const char *src = luaL_checklstring(L, 1, &len); char *pem = luaL_checkstring(L, 2); SHA_CTX c; unsigned char sha[SHA_DIGEST_LENGTH]; memset(sha, 0, SHA_DIGEST_LENGTH); if(SHA_Init(&c) != 1) { OPENSSL_cleanse(&c, sizeof(c)); return luaL_error(L, "SHA init error"); } if(SHA1_Update(&c, src, len) != 1) { OPENSSL_cleanse(&c, sizeof(c)); return luaL_error(L, "SHA update error"); } if(SHA1_Final(sha, &c) != 1) { OPENSSL_cleanse(&c, sizeof(c)); return luaL_error(L, "SHA update error"); } OPENSSL_cleanse(&c, sizeof(c)); BIO *bio = BIO_new_mem_buf((void *)pem, -1); if(bio == NULL) { BIO_free_all(bio); return luaL_error(L, "PEM error"); } RSA *rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL); if(rsa == NULL) { BIO_free_all(bio); return luaL_error(L, "RSA read private key error"); } BIO_free_all(bio); int n = RSA_size(rsa), wn; char dst[n]; memset(dst, 0, n); int ret = RSA_sign(NID_sha1, (unsigned char *)sha, SHA_DIGEST_LENGTH, (unsigned char *)dst, (unsigned int *)&wn, rsa); if(ret != 1) { RSA_free(rsa); BIO_free_all(bio); return luaL_error(L, "RSA sign error"); } RSA_free(rsa); lua_pushlstring(L, dst, wn); return 1; }
static void cyon_store_map(void) { struct stat st; int fd; struct store_header header; char fpath[MAXPATHLEN], *hex; u_char hash[SHA_DIGEST_LENGTH]; u_char ohash[SHA_DIGEST_LENGTH]; if (store_nopersist) return; snprintf(fpath, sizeof(fpath), CYON_STORE_FILE, storepath, storename); if ((fd = open(fpath, O_RDONLY)) == -1) { if (errno != ENOENT) fatal("open(%s): %s", fpath, errno_s); cyon_storelog_replay_all(); return; } if (fstat(fd, &st) == -1) fatal("cyon_store_map(): fstat(): %s", errno_s); SHA_Init(&shactx); memset(&header, 0, sizeof(header)); cyon_atomic_read(fd, &header, sizeof(header), &shactx, 0); if (header.flags & STORE_HAS_PASSPHRASE) { store_passphrase = cyon_malloc(SHA256_DIGEST_LENGTH); cyon_atomic_read(fd, store_passphrase, SHA256_DIGEST_LENGTH, &shactx, 0); } rnode = cyon_malloc(sizeof(struct node)); cyon_atomic_read(fd, rnode, sizeof(struct node), &shactx, 0); cyon_store_mapnode(fd, rnode); SHA_Final(hash, &shactx); cyon_atomic_read(fd, ohash, sizeof(ohash), NULL, 0); close(fd); if (memcmp(hash, ohash, SHA_DIGEST_LENGTH)) fatal("SHA1 checksum mismatch, store corrupted?"); memcpy(store_state, hash, SHA_DIGEST_LENGTH); if (store_retain_logs) { cyon_sha_hex(store_state, &hex); cyon_log(LOG_NOTICE, "store state is %s", hex); cyon_mem_free(hex); } cyon_storelog_replay_all(); }
static int crypt_all(int *pcount, struct db_salt *salt) { int count = *pcount; SHA_Init( &ctx ); SHA_Update( &ctx, (unsigned char *) saved_key, strlen( saved_key ) ); SHA_Final( (unsigned char *) crypt_key, &ctx); return count; }
int main(int argc, char *argv[]) { int i,err=0; unsigned char **P,**R; static unsigned char buf[1000]; char *p,*r; SHA_CTX c; unsigned char md[SHA_DIGEST_LENGTH]; #ifdef CHARSET_EBCDIC ebcdic2ascii(test[0], test[0], strlen(test[0])); ebcdic2ascii(test[1], test[1], strlen(test[1])); #endif P=(unsigned char **)test; R=(unsigned char **)ret; i=1; while (*P != NULL) { p=pt(SHA(*P,(unsigned long)strlen((char *)*P),NULL)); if (strcmp(p,(char *)*R) != 0) { printf("error calculating SHA on '%s'\n",*P); printf("got %s instead of %s\n",p,*R); err++; } else printf("test %d ok\n",i); i++; R++; P++; } memset(buf,'a',1000); #ifdef CHARSET_EBCDIC ebcdic2ascii(buf, buf, 1000); #endif /*CHARSET_EBCDIC*/ SHA_Init(&c); for (i=0; i<1000; i++) SHA_Update(&c,buf,1000); SHA_Final(md,&c); p=pt(md); r=bigret; if (strcmp(p,r) != 0) { printf("error calculating SHA on '%s'\n",p); printf("got %s instead of %s\n",p,r); err++; } else printf("test 3 ok\n"); exit(err); return(0); }
std::string Digestor::Sha1str(const std::string &data) { unsigned char sha1hash[20] = {0}; SHA_CTX sha1ctx; SHA_Init(&sha1ctx); SHA_Update(&sha1ctx, data.c_str(), data.length()); SHA_Final(sha1hash, &sha1ctx); std::string digest = BinaryHashToHexString(sha1hash, 20); return digest; }
unsigned char *SHA(const unsigned char *d, unsigned long n, unsigned char *md) { SHA_CTX c; static unsigned char m[SHA_DIGEST_LENGTH]; if (md == NULL) md=m; SHA_Init(&c); SHA_Update(&c,d,n); SHA_Final(md,&c); memset(&c,0,sizeof(c)); return(md); }
static void *dss_createkey(unsigned char *pub_blob, int pub_len, unsigned char *priv_blob, int priv_len) { dss_key *dss; char *pb = (char *) priv_blob; char *hash; int hashlen; SHA_State s; unsigned char digest[20]; Bignum ytest; dss = dss_newkey((char *) pub_blob, pub_len); if (!dss) return NULL; dss->x = getmp(&pb, &priv_len); if (!dss->x) { dss_freekey(dss); return NULL; } /* * Check the obsolete hash in the old DSS key format. */ hashlen = -1; getstring(&pb, &priv_len, &hash, &hashlen); if (hashlen == 20) { SHA_Init(&s); sha_mpint(&s, dss->p); sha_mpint(&s, dss->q); sha_mpint(&s, dss->g); SHA_Final(&s, digest); if (0 != memcmp(hash, digest, 20)) { dss_freekey(dss); return NULL; } } /* * Now ensure g^x mod p really is y. */ ytest = modpow(dss->g, dss->x, dss->p); if (0 != bignum_cmp(ytest, dss->y)) { dss_freekey(dss); freebn(ytest); return NULL; } freebn(ytest); return dss; }
/*{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*} * Name: SH_Mngr * Desc: Run Soil simulation. Will run the Duff or the Exp simulation * code, based on presence of Duff. * Note Duff Sim: * The Fuel has to be run before coming here, because the Fuel * calculates the Post Duff Depth which DUff Sim needs to run. * Note Exp Heat: * The Fuel has to be run before coming here, which should * have detected the no Duff Depth/Load and run burnup which calculates * the heat and time need by Exp Heat. * Note-1: There use to be some Error_Window() type logic errors in the * soil code, it would have been hard and not worth it to do them * thru the functions so I did a global string. * In: a_SI...... * * Ret: 1 Ok, 0 Error {*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{*}{**/ int WINAPI SH_Mngr (d_SI *a_SI, d_SO *a_SO, char cr_TmpFN[], char cr_ErrMes[]) { d_SD s_SD; d_SE s_SE; char cr[40]; strcpy (gcr_SoiErr,""); SO_Init (a_SO); /* Init the output struct */ if ( !xstrcmpi(a_SI->cr_BrnIg,"NO")) { /* Burnup ran & didn't ignite */ SHA_Init_0 (); /* so 0 out this arrary so that */ return 1; } /* 0s come out in the report */ SHA_Init (); /* Init the Soil Heat Temp Array*/ if ( a_SI->f_DufDepPre > 0 ) /* Prefire Duff depth determines*/ goto DuffSim; /* if we run Duff or Exp simulat*/ /*.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.*/ /* Do the Exp Heat, because there is no duff depth */ strcpy (a_SO->cr_Model,e_SM_ZDuff); /* tells what soil model */ if ( !SE_Init (a_SI, &s_SE, cr_ErrMes)) /* Ready the SE input struct */ return 0; if ( !SE_Mngr (&s_SE,cr_TmpFN,cr_ErrMes)) /* Run it, makes Pt arrar& File */ return 0; goto Load; /*.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.*/ /* Do the Duff Simulation Modes */ DuffSim: strcpy (a_SO->cr_Model,e_SM_Duff); if ( !SD_ChkSoiDuf(a_SI->f_DufDepPre, a_SI->f_DufDepPos, cr_ErrMes) ) return 0; if ( !SD_Init (&s_SD, a_SI, cr_ErrMes)) return 0; /* Duff Sim is done when there is a Duff Depth to use....................... */ if ( !SD_Mngr(&s_SD,cr_TmpFN,cr_ErrMes)) /* Run Soil Duff Simulation */ return 0; Load: SO_Load (a_SI, a_SO); /* Get Soil Outputs */ if ( strcmp (gcr_SoiErr,"") ) { /* See Note-1 above */ strcpy (cr_ErrMes,gcr_SoiErr); return 0; } return 1; }
unsigned char *SHA(const unsigned char *d, unsigned long n, unsigned char *md) { SHA_CTX c; static unsigned char m[SHA_DIGEST_LENGTH]; if (md == NULL) md=m; if (!SHA_Init(&c)) return NULL; SHA_Update(&c,d,n); SHA_Final(md,&c); OPENSSL_cleanse(&c,sizeof(c)); return(md); }
/** * SHA1WithRSA公钥验签 * * LUA示例: * local codec = require('codec') * local src = 'something' * local sign = [[...]] --BASE64签名 * local bs = codec.base64_decode(sign) * local pem = [[...]] --公钥PEM字符串 * local type = 1 * local ok = codec.rsa_public_verify(src, bs, pem, type) --true/false */ static int codec_rsa_public_verify(lua_State *L) { size_t srclen, signlen; const char *src = luaL_checklstring(L, 1, &srclen); const char *sign = luaL_checklstring(L, 2, &signlen); char *pem = luaL_checkstring(L, 3); int type = luaL_checkint(L, 4); SHA_CTX ctx; int ctxlen = sizeof(ctx); unsigned char sha[SHA_DIGEST_LENGTH]; memset(sha, 0, SHA_DIGEST_LENGTH); if(SHA_Init(&ctx) != 1) { OPENSSL_cleanse(&ctx, ctxlen); return luaL_error(L, "SHA init error"); } if(SHA1_Update(&ctx, src, srclen) != 1) { OPENSSL_cleanse(&ctx, ctxlen); return luaL_error(L, "SHA update error"); } if(SHA1_Final(sha, &ctx) != 1) { OPENSSL_cleanse(&ctx, ctxlen); return luaL_error(L, "SHA update error"); } OPENSSL_cleanse(&ctx, ctxlen); BIO *bio = BIO_new_mem_buf((void *)pem, -1); if(bio == NULL) { BIO_free_all(bio); return luaL_error(L, "PEM error"); } RSA *rsa = type == 1 ? PEM_read_bio_RSAPublicKey(bio, NULL, NULL, NULL) : PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL); if(rsa == NULL) { BIO_free_all(bio); return luaL_error(L, "RSA read public key error"); } BIO_free_all(bio); int ret = RSA_verify(NID_sha1, sha, SHA_DIGEST_LENGTH, (unsigned char *)sign, signlen, rsa); RSA_free(rsa); lua_pushboolean(L, ret); return 1; }
void cyon_store_init(void) { char *hex; lfd = -1; dfd = -1; rnode = NULL; key_count = 0; store_errors = 0; store_log_offset = 0; store_passphrase = NULL; SHA_Init(&shactx); SHA_Final(store_state, &shactx); pthread_rwlock_init(&store_lock, NULL); cyon_store_map(); if (rnode == NULL) { cyon_log(LOG_NOTICE, "store is empty, starting a new one"); if (store_retain_logs) { cyon_sha_hex(store_state, &hex); cyon_log(LOG_NOTICE, "new state is %s", hex); cyon_mem_free(hex); } rnode = cyon_malloc(sizeof(struct node)); memset(rnode, 0, sizeof(struct node)); } else { cyon_log(LOG_NOTICE, "store loaded from disk with %ld keys", key_count); } if (store_errors) { cyon_log(LOG_ERR, "INCONSISTENCIES IN STORE LOG/DATA FILE"); cyon_log(LOG_ERR, "THESE MUST BE REPAIRED. FORCING READONLY"); cyon_readonly_mode = 1; } if (!store_nopersist && !cyon_readonly_mode) cyon_storelog_reopen(0); if (cyon_readonly_mode) cyon_log(LOG_NOTICE, "Cyon is in read-only mode"); }
void do_fp(TINYCLR_SSL_FILE *f) { SHA_CTX c; unsigned char md[SHA_DIGEST_LENGTH]; int fd; int i; unsigned char buf[BUFSIZE]; fd=TINYCLR_SSL_FILENO(f); SHA_Init(&c); for (;;) { i=read(fd,buf,BUFSIZE); if (i <= 0) break; SHA_Update(&c,buf,(unsigned long)i); } SHA_Final(&(md[0]),&c); pt(md); }
static void SHA_File(FILE *file, unsigned char **output, int *outlength) { *output = new unsigned char[SHA_DIGEST_LENGTH]; *outlength = SHA_DIGEST_LENGTH; SHA_CTX c; int i; unsigned char buf[SHA_FILE_BUFFER_SIZE]; SHA_Init(&c); for (;;) { i = fread(buf,1,SHA_FILE_BUFFER_SIZE,file); if(i <= 0) break; SHA_Update(&c,buf,(unsigned long)i); } SHA_Final(*output, &c); }
void cyon_storelog_write(u_int8_t op, u_int8_t *key, u_int32_t klen, u_int8_t *data, u_int32_t dlen, u_int32_t flags) { u_int32_t len; struct store_log *slog; u_int8_t *buf, *p; if (store_nopersist) return; len = sizeof(struct store_log) + klen + dlen; buf = cyon_malloc(len); slog = (struct store_log *)buf; memset(slog, 0, sizeof(*slog)); slog->op = op; slog->klen = klen; slog->dlen = dlen; slog->flags = flags; memcpy(slog->magic, store_log_magic, 4); p = buf + sizeof(*slog); memcpy(p, key, slog->klen); if (dlen > 0) memcpy(p + slog->klen, data, dlen); SHA_Init(&shactx); SHA_Update(&shactx, buf, len); SHA_Final(slog->hash, &shactx); cyon_atomic_write(lfd, buf, len, NULL); cyon_mem_free(buf); log_modified = 1; store_modified = 1; store_log_offset += len; }
static int init(EVP_MD_CTX *ctx) { return SHA_Init(ctx->md_data); }
pid_t cyon_store_write(void) { pid_t pid; u_int8_t *buf; struct store_header header; int fd, ret; u_int32_t len, blen; u_char hash[SHA_DIGEST_LENGTH]; char fpath[MAXPATHLEN], tpath[MAXPATHLEN]; if (rnode == NULL || store_modified == 0 || store_nopersist) { cyon_log(LOG_NOTICE, "store is clean, not writing"); return (CYON_RESULT_OK); } /* * The write lock protects us from getting new entries in the log * so it is safe to reopen the logs after the fork. */ cyon_store_lock(1); pid = fork(); if (pid == -1) { cyon_store_unlock(); cyon_log(LOG_NOTICE, "store write not started (fork: %s)", errno_s); return (CYON_RESULT_ERROR); } if (pid != 0) { store_modified = 0; if (!cyon_readonly_mode) cyon_storelog_reopen(1); cyon_store_unlock(); cyon_log(LOG_NOTICE, "store write started (%d)", pid); return (pid); } if (!cyon_readonly_mode) close(lfd); snprintf(fpath, sizeof(fpath), CYON_STORE_FILE, storepath, storename); snprintf(tpath, sizeof(tpath), CYON_STORE_TMPFILE, storepath, storename); fd = open(tpath, O_CREAT | O_TRUNC | O_WRONLY, 0700); if (fd == -1) fatal("open(%s): %s", tpath, errno_s); memset(&header, 0, sizeof(header)); if (store_passphrase != NULL) header.flags |= STORE_HAS_PASSPHRASE; len = 0; blen = 128 * 1024 * 1024; buf = cyon_malloc(blen); memcpy(buf, &header, sizeof(header)); len += sizeof(header); if (header.flags & STORE_HAS_PASSPHRASE) { memcpy(buf + len, store_passphrase, SHA256_DIGEST_LENGTH); len += SHA256_DIGEST_LENGTH; } SHA_Init(&shactx); cyon_store_writenode(fd, rnode, buf, blen, &len, NULL); if (len > 0) cyon_atomic_write(fd, buf, len, &shactx); cyon_mem_free(buf); SHA_Final(hash, &shactx); cyon_atomic_write(fd, hash, SHA_DIGEST_LENGTH, NULL); for (;;) { ret = fsync(fd); if (ret == -1 && errno == EINTR) continue; if (ret == -1) fatal("store write failed %s", errno_s); break; } close(fd); if (rename(tpath, fpath) == -1) fatal("cannot move store into place: %s", errno_s); exit(0); }
static void crypt_all(int count) { SHA_Init( &ctx ); SHA_Update( &ctx, (unsigned char *) saved_key, strlen( saved_key ) ); SHA_Final( (unsigned char *) crypt_key, &ctx); }
int cyon_storelog_replay(char *state, int when) { struct stat st; long offset; u_int64_t len, olen; struct store_log slog, *plog; u_int64_t added, removed; char fpath[MAXPATHLEN], *hex; u_char hash[SHA_DIGEST_LENGTH]; u_int8_t *buf, err, ch, *key, *data; snprintf(fpath, sizeof(fpath), CYON_LOG_FILE, storepath, storename, state); if ((lfd = open(fpath, O_RDONLY)) == -1) { if (errno == ENOENT) return (CYON_RESULT_ERROR); fatal("open(%s): %s", fpath, errno_s); } if (fstat(lfd, &st) == -1) fatal("fstat(): %s", errno_s); if (st.st_size == 0) { close(lfd); if (when == CYON_REPLAY_STARTUP) return (CYON_RESULT_ERROR); return (CYON_RESULT_OK); } olen = 0; buf = NULL; offset = 0; replaying_log = 1; added = removed = 0; if (when == CYON_REPLAY_REQUEST) store_errors = 0; cyon_log(LOG_NOTICE, "applying log %s", fpath); for (;;) { while (offset < st.st_size) { cyon_atomic_read(lfd, &ch, 1, NULL, 0); offset++; if (ch != store_log_magic[0]) continue; if ((long)(offset + sizeof(slog) - 1) >= st.st_size) break; cyon_atomic_read(lfd, &slog.magic[1], sizeof(slog) - 1, NULL, 0); offset += sizeof(slog) - 1; slog.magic[0] = ch; if (!memcmp(slog.magic, store_log_magic, 4)) break; } if (offset >= st.st_size) break; if ((offset + slog.dlen + slog.klen) > st.st_size) { store_errors++; cyon_log(LOG_NOTICE, "LOG CORRUPTED, would read past at %ld", offset); continue; } len = slog.klen + slog.dlen + sizeof(slog); if (len > olen) { if (buf != NULL) cyon_mem_free(buf); buf = cyon_malloc(len); } olen = len; memcpy(buf, &slog, sizeof(slog)); cyon_atomic_read(lfd, buf + sizeof(slog), len - sizeof(slog), NULL, 0); offset += slog.klen + slog.dlen; plog = (struct store_log *)buf; memcpy(hash, plog->hash, SHA_DIGEST_LENGTH); memset(plog->hash, '\0', SHA_DIGEST_LENGTH); SHA_Init(&shactx); SHA_Update(&shactx, buf, len); SHA_Final(plog->hash, &shactx); if (memcmp(hash, plog->hash, SHA_DIGEST_LENGTH)) { store_errors++; cyon_log(LOG_NOTICE, "INCORRECT CHECKSUM for log @ %ld, skipping", offset); continue; } key = buf + sizeof(slog); if (slog.dlen > 0) data = buf + sizeof(slog) + slog.klen; else data = NULL; if (rnode == NULL) { rnode = cyon_malloc(sizeof(struct node)); memset(rnode, 0, sizeof(struct node)); } store_modified = 1; switch (slog.op) { case CYON_OP_SETAUTH: if (slog.klen != SHA256_DIGEST_LENGTH) { cyon_log(LOG_NOTICE, "replay of setauth log entry failed"); break; } if (store_passphrase != NULL) cyon_mem_free(store_passphrase); store_passphrase = cyon_malloc(slog.klen); memcpy(store_passphrase, key, slog.klen); break; case CYON_OP_PUT: if (!cyon_store_put(key, slog.klen, data, slog.dlen, slog.flags, &err)) { if (when != CYON_REPLAY_REQUEST) fatal("replay failed at this stage?"); } added++; break; case CYON_OP_DEL: if (!cyon_store_del(key, slog.klen, &err)) { if (when != CYON_REPLAY_REQUEST) fatal("replay failed at this stage?"); } removed++; break; case CYON_OP_REPLACE: if (!cyon_store_replace(key, slog.klen, data, slog.dlen, &err)) { if (when != CYON_REPLAY_REQUEST) fatal("replay failed at this stage?"); } break; default: store_errors++; printf("unknown log operation %d\n", slog.op); break; } } if (buf != NULL) cyon_mem_free(buf); if (store_errors) { cyon_log(LOG_NOTICE, "LOG REPLAY *FAILED*, SEE ERRORS ABOVE"); if (when == CYON_REPLAY_REQUEST) { cyon_readonly_mode = 1; cyon_log(LOG_NOTICE, "FORCING READONLY MODE"); } } else { cyon_log(LOG_NOTICE, "log replay completed: %ld added, %ld removed", added, removed); } close(lfd); replaying_log = 0; if (!store_errors && store_retain_logs) { cyon_store_current_state(store_state); cyon_sha_hex(store_state, &hex); cyon_log(LOG_NOTICE, "store state is %s", hex); } return ((store_errors) ? CYON_RESULT_ERROR : CYON_RESULT_OK); }
void plSHAChecksum::Start() { SHA_Init(&fContext); fValid = false; }
SHA0Stream::SHA0Stream(Stream::ptr parent, bool own) : HashStream(parent, own) { SHA_Init(&m_ctx); }
/* * The Mines (among others) game descriptions contain the location of every * mine, and can therefore be used to cheat. * * It would be pointless to attempt to _prevent_ this form of * cheating by encrypting the description, since Mines is * open-source so anyone can find out the encryption key. However, * I think it is worth doing a bit of gentle obfuscation to prevent * _accidental_ spoilers: if you happened to note that the game ID * starts with an F, for example, you might be unable to put the * knowledge of those mines out of your mind while playing. So, * just as discussions of film endings are rot13ed to avoid * spoiling it for people who don't want to be told, we apply a * keyless, reversible, but visually completely obfuscatory masking * function to the mine bitmap. */ void obfuscate_bitmap(unsigned char *bmp, int bits, int decode) { int bytes, firsthalf, secondhalf; struct step { unsigned char *seedstart; int seedlen; unsigned char *targetstart; int targetlen; } steps[2]; int i, j; /* * My obfuscation algorithm is similar in concept to the OAEP * encoding used in some forms of RSA. Here's a specification * of it: * * + We have a `masking function' which constructs a stream of * pseudorandom bytes from a seed of some number of input * bytes. * * + We pad out our input bit stream to a whole number of * bytes by adding up to 7 zero bits on the end. (In fact * the bitmap passed as input to this function will already * have had this done in practice.) * * + We divide the _byte_ stream exactly in half, rounding the * half-way position _down_. So an 81-bit input string, for * example, rounds up to 88 bits or 11 bytes, and then * dividing by two gives 5 bytes in the first half and 6 in * the second half. * * + We generate a mask from the second half of the bytes, and * XOR it over the first half. * * + We generate a mask from the (encoded) first half of the * bytes, and XOR it over the second half. Any null bits at * the end which were added as padding are cleared back to * zero even if this operation would have made them nonzero. * * To de-obfuscate, the steps are precisely the same except * that the final two are reversed. * * Finally, our masking function. Given an input seed string of * bytes, the output mask consists of concatenating the SHA-1 * hashes of the seed string and successive decimal integers, * starting from 0. */ bytes = (bits + 7) / 8; firsthalf = bytes / 2; secondhalf = bytes - firsthalf; steps[decode ? 1 : 0].seedstart = bmp + firsthalf; steps[decode ? 1 : 0].seedlen = secondhalf; steps[decode ? 1 : 0].targetstart = bmp; steps[decode ? 1 : 0].targetlen = firsthalf; steps[decode ? 0 : 1].seedstart = bmp; steps[decode ? 0 : 1].seedlen = firsthalf; steps[decode ? 0 : 1].targetstart = bmp + firsthalf; steps[decode ? 0 : 1].targetlen = secondhalf; for (i = 0; i < 2; i++) { SHA_State base, final; unsigned char digest[20]; char numberbuf[80]; int digestpos = 20, counter = 0; SHA_Init(&base); SHA_Bytes(&base, steps[i].seedstart, steps[i].seedlen); for (j = 0; j < steps[i].targetlen; j++) { if (digestpos >= 20) { sprintf(numberbuf, "%d", counter++); final = base; SHA_Bytes(&final, numberbuf, strlen(numberbuf)); SHA_Final(&final, digest); digestpos = 0; } steps[i].targetstart[j] ^= digest[digestpos++]; }
int main(void) { struct MD5Context md5c; SHA_State sha1s; unsigned char keybuf[20], testbuf[64]; int i, j; char *p; static char *test[]={ "", "a", "abc", "message digest", "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "12345678901234567890123456789012345678901234567890123456789012345678901234567890", }; static char *md5[]={ "d41d8cd98f00b204e9800998ecf8427e", "0cc175b9c0f1b6a831c399e269772661", "900150983cd24fb0d6963f7d28e17f72", "f96b697d7cb7938d525a2f31aaf161d0", "c3fcd3d76192e4007dfb496cca67e13b", "d174ab98d277d9f5a5611c2c9f419d9f", "57edf4a22be3c955ac49da2e2107b67a", }; static char *sha1[]={ "da39a3ee5e6b4b0d3255bfef95601890afd80709", "86f7e437faa5a7fce15d1ddcb9eaeaea377667b8", "a9993e364706816aba3e25717850c26c9cd0d89d", "c12252ceda8be8994d5fa0290a47231c1d16aae3", "32d10c7b8cf96570ca04ce37f2a19d84240d3a89", "761c457bf73b14d27e9e9265c46f4b4dda11f940", "50abf5706a150990a08b2c5ea40fa0e585554732", }; printf("testing MD5...\n"); for (i = 0; i < sizeof(test) / sizeof(char *); i++) { MD5Init(&md5c); MD5Update(&md5c, test[i], strlen(test[i])); MD5Final(keybuf, &md5c); for (j = 0, p = testbuf; j < 16; j++, p += 2) sprintf(p, "%02x", keybuf[j]); printf("test %d %s!\n", i + 1, strncmp(md5[i], testbuf, sizeof(testbuf)) ? "failed" : "ok"); } printf("\ntesting SHA1...\n"); for (i = 0; i < sizeof(test) / sizeof(char *); i++) { SHA_Init(&sha1s); SHA_Bytes(&sha1s, test[i], strlen(test[i])); SHA_Final(&sha1s, keybuf); for (j = 0, p = testbuf; j < 20; j++, p += 2) sprintf(p, "%02x", keybuf[j]); printf("test %d %s!\n", i + 1, strncmp(sha1[i], testbuf, sizeof(testbuf)) ? "failed" : "ok"); } return 0; }
CGUL::SHA::~SHA() { SHA_Init(ctx); }