int
main(void)
{
    SXE_HASH    * hash;
    unsigned      i;
    unsigned      id;
    unsigned      bucket;
    int           counter[MAX_BUCKET_INDEX];

    plan_tests(2);
    sxe_log_set_level(SXE_LOG_LEVEL_DEBUG);

    memset(counter, 0, MAX_BUCKET_INDEX * sizeof(int));
    hash = sxe_hash_new("test-hash", HASH_SIZE);

    for (i = 0; i < HASH_SIZE; i++)
    {
        char in_buf[5];
        char out_buf[41];

        snprintf(in_buf, 5, "%d", i);
        sha1sum( in_buf, 4, out_buf);
        id = sxe_hash_set(hash, out_buf, SXE_HASH_SHA1_AS_HEX_LENGTH, 1U);
        bucket = sxe_pool_index_to_state(hash, id);
        SXEA11(bucket < MAX_BUCKET_INDEX, "Bucket index %u is out of range", bucket);
        counter[bucket]++;

        if (counter[bucket] > MAX_ALLOWED_PER_BUCKET_INDEX) {
            diag("Count at bucket index %u is greater than %u", counter[bucket], MAX_ALLOWED_PER_BUCKET_INDEX);
            break;
        }
    }

    is(i, HASH_SIZE, "%u items SHA1 hashed and no bucket has more that %u entries", i, MAX_ALLOWED_PER_BUCKET_INDEX);

    memset(counter, 0, MAX_BUCKET_INDEX * sizeof(int));
    hash = sxe_hash_new_plus("lookup3", HASH_SIZE, sizeof(SXE_HASH), 0, 8, SXE_HASH_OPTION_LOOKUP3_HASH);

    for (i = 0; i < HASH_SIZE; i++)
    {
        id = sxe_hash_take(hash);
        snprintf((char *)&hash[id], 9, "%08x", i);
        sxe_hash_add(hash, id);

        bucket = sxe_pool_index_to_state(hash, id);
        SXEA11(bucket < MAX_BUCKET_INDEX, "Bucket index %u is out of range", bucket);
        counter[bucket]++;

        if (counter[bucket] > MAX_ALLOWED_PER_BUCKET_INDEX + 1) {
            diag("Count at bucket index %u is greater than %u", counter[bucket], MAX_ALLOWED_PER_BUCKET_INDEX + 1);
            break;
        }
    }

    is(i, HASH_SIZE, "%u items lookup3 hashed and no bucket has more that %u entries", i, MAX_ALLOWED_PER_BUCKET_INDEX + 1);
    return exit_status();
}
예제 #2
0
파일: websocket.c 프로젝트: bpa/gamed
unsigned char *sha1_base64(const unsigned char *data) {
	unsigned char *base64 = NULL;
	unsigned char *sum = sha1sum(data);
	base64 = base64_encode(sum);
	free(sum);
	return base64;
}
예제 #3
0
파일: password.c 프로젝트: cmouse/hbs
void user_command_password (NICK * nick, CHANNEL * channel, const char *cmd,
			    const char **args, int argc)
{
  char *newpw;
  if (channel)
    {
      puttext
	("NOTICE %s :Do not use this command in a channel - logging this so you will get spanked with a wide SCSI cable\r\n",
	 nick->nick);
      print ("SECURITY ALERT: %s[%s] tried to change password on a channel",
	     nick->nick, nick->user->user);
      return;
    }
  if (argc < 2)
    {
      puttext
	("NOTICE %s :Usage: %s [old password] [new password] [new password]\r\n",
	 nick->nick, cmd);
      puttext
	("NOTICE %s :Usage: %s [new password] [new password] (if you do not have a password)\r\n",
	 nick->nick, cmd);
      return;
    }
  if (strlen (nick->user->pass) > 0)
    {
      if (!user_check_pass (nick->user, args[0]))
	{
	  puttext ("NOTICE %s :Old password did not match - event logged\r\n",
		   nick->nick);
	  print
	    ("SECURITY ALERT: %s[%s] tried to change password - wrong old password",
	     nick->nick, nick->user->user);
	  return;
	}
      if ((argc < 3) || strcmp (args[1], args[2]))
	{
	  puttext ("NOTICE %s :New passwords did not match\r\n", nick->nick);
	  return;
	}
    }
  else
    {
      if (strcmp (args[0], args[1]))
	{
	  puttext ("NOTICE %s :New passwords did not match\r\n", nick->nick);
	  return;
	}
    }
  newpw = sha1sum (args[1]);
  memcpy (nick->user->pass, newpw, SHA_DIGEST_LENGTH);
  puttext ("NOTICE %s :Password changed\r\n", nick->nick);
  print ("%s!%s@%s[%s] changed password", nick->nick, nick->ident, nick->host,
	 nick->user->user);
  free (newpw);
}
	void integrate_adaptive_v1()
	{
		state_type X=start_state;
		odev1::Solver<odev1::Steppers::RKCK54,CSystem> solver_v1(std::ref(*this));
		solver_v1.integrate_adaptive(
			X,// is manipulated
			start_time ,
			stop_time ,
			initial_dt);
		results_finalize();
		sha1=sha1sum(results).signature();
	}
예제 #5
0
파일: file.c 프로젝트: lhjay1/h2o
void test_lib__file_c()
{
    h2o_globalconf_t globalconf;
    h2o_hostconf_t *hostconf;
    h2o_context_t ctx;

    h2o_config_init(&globalconf);
    hostconf = h2o_config_register_host(&globalconf, "default");
    h2o_file_register(hostconf, "/", "t/00unit/file", NULL, NULL);

    h2o_context_init(&ctx, test_loop, &globalconf);

    {
        h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx);
        conn->req.method = h2o_buf_init(H2O_STRLIT("GET"));
        conn->req.path = h2o_buf_init(H2O_STRLIT("/"));
        h2o_loopback_run_loop(conn);
        ok(conn->req.res.status == 200);
        ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_TYPE, "text/html"));
        ok(h2o_memis(conn->body->bytes, conn->body->size, H2O_STRLIT("hello html\n")));
        h2o_loopback_destroy(conn);
    }
    {
        h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx);
        conn->req.method = h2o_buf_init(H2O_STRLIT("GET"));
        conn->req.path = h2o_buf_init(H2O_STRLIT("/index.html"));
        h2o_loopback_run_loop(conn);
        ok(conn->req.res.status == 200);
        ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_TYPE, "text/html"));
        ok(h2o_memis(conn->body->bytes, conn->body->size, H2O_STRLIT("hello html\n")));
        h2o_loopback_destroy(conn);
    }
    {
        h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx);
        conn->req.method = h2o_buf_init(H2O_STRLIT("GET"));
        conn->req.path = h2o_buf_init(H2O_STRLIT("/1000.txt"));
        h2o_loopback_run_loop(conn);
        ok(conn->req.res.status == 200);
        ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_TYPE, "text/plain"));
        ok(conn->body->size == 1000);
        ok(strcmp(sha1sum(conn->body->bytes, conn->body->size), "dfd3ae1f5c475555fad62efe42e07309fa45f2ed") == 0);
        h2o_loopback_destroy(conn);
    }
    {
        h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx);
        conn->req.method = h2o_buf_init(H2O_STRLIT("GET"));
        conn->req.path = h2o_buf_init(H2O_STRLIT("/1000000.txt"));
        h2o_loopback_run_loop(conn);
        ok(conn->req.res.status == 200);
        ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_TYPE, "text/plain"));
        ok(conn->body->size == 1000000);
        ok(strcmp(sha1sum(conn->body->bytes, conn->body->size), "00c8ab71d0914dce6a1ec2eaa0fda0df7044b2a2") == 0);
        h2o_loopback_destroy(conn);
    }
    {
        h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx);
        conn->req.method = h2o_buf_init(H2O_STRLIT("GET"));
        conn->req.path = h2o_buf_init(H2O_STRLIT("/index_txt/"));
        h2o_loopback_run_loop(conn);
        ok(conn->req.res.status == 200);
        ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_TYPE, "text/plain"));
        ok(h2o_memis(conn->body->bytes, conn->body->size, H2O_STRLIT("hello text\n")));
        h2o_loopback_destroy(conn);
    }
    {
        h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx);
        conn->req.method = h2o_buf_init(H2O_STRLIT("GET"));
        conn->req.path = h2o_buf_init(H2O_STRLIT("/index_txt"));
        h2o_loopback_run_loop(conn);
        ok(conn->req.res.status == 301);
        ok(check_header(&conn->req.res, H2O_TOKEN_LOCATION, "http://default/index_txt/"));
        h2o_loopback_destroy(conn);
    }
    {
        h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx);
        conn->req.method = h2o_buf_init(H2O_STRLIT("GET"));
        conn->req.path = h2o_buf_init(H2O_STRLIT("/index_txt_as_dir/"));
        h2o_loopback_run_loop(conn);
        ok(conn->req.res.status == 301);
        ok(check_header(&conn->req.res, H2O_TOKEN_LOCATION, "http://default/index_txt_as_dir/index.txt/"));
        h2o_loopback_destroy(conn);
    }

    h2o_context_dispose(&ctx);
    h2o_config_dispose(&globalconf);
}
예제 #6
0
void
Active_Poll()
{
	static int leaving_bytes = 4;

	if (activated == 0)
		return;

	if (USE_FORKS == 0)
	{
		dooutput2();

		doinput2();
	}
	else
	{
		char buf[BUFSIZ];
		char *buffer;
		register int c, i, t;

		while ((c = read(xfer_fifo[0], buf, sizeof(buf))) > 0)
		{

			fwrite(buf, 1, c, log);
			fflush(log);

			Packet_Reset(pktbuf);

			TRACE(Neuro_s("Sending a packet NET_DATA of %d bytes, checksum %s", c, sha1sum((const unsigned char*)buf, c, NULL)));
			Packet_Push32(pktbuf, NET_DATA);
			Packet_Push32(pktbuf, 0); /* Client_SendPacket sets the size of the packet */
			Packet_PushString(pktbuf, c, buf);

			Client_SendPacket(Packet_GetBuffer(pktbuf), Packet_GetLen(pktbuf));

#if temp
			/* we look for the EOT in the last bytes of the buffer */
			if (buf[c - 1] == 4)
			{
				/* printf("FOUND ONE!!!\n"); */
				i = c;
				while (buf[--i] == 4)
				{
					/* printf("LEN %d removed one %d left char %d\n", c, leaving_bytes, buf[i]); */
					leaving_bytes--;
				}

				if (leaving_bytes <= 0)
				{
					TRACE("PIPE EXIT PACKET RECIEVED");
					Main_Exit();
					return;
				}
			}
			else
			{
				/* printf("LEN %d  %d \'%d\'\n", c, buf[c], '\r'); */
				leaving_bytes = 4;
				/* break; */
			}

			buffer = buf;

			t = 0;
			i = c;
			while (c > 0)
			{
				i = c;
				if (i > 490)
					i = 490;

				/* write an output to a file called log_file to debug the packets sent to the server. */
				/* fprintf(log, "c %d i %d t %d --> buffer \"%s\"\n", c, i, t, buffer); */
				fprintf(log, "c %d i %d t %d --> buffer \"", c, i, t);
				fwrite(buf, 1, i, log);
				fprintf(log, "\"\n");
				fflush(log);

				t += i;

				c -= i;

				Packet_Reset(pktbuf);

				Packet_Push32(pktbuf, NET_DATA);
				Packet_Push32(pktbuf, 0);
				Packet_Push32(pktbuf, i);
				Packet_PushString(pktbuf, i, buffer);

				if (Packet_GetLen(pktbuf) > (4 + 4 + i))
				{
					ERROR(Neuro_s("Packet bigger than it should by %d bytes", Packet_GetLen(pktbuf) - 200));
				}

				Client_SendPacket(Packet_GetBuffer(pktbuf), Packet_GetLen(pktbuf));
				buffer = &buf[t];
			}

			/* temporarily... this is not necessary */
			memset(buf, 0, t);
#endif /* temp */
		}

		if (c == -1 && errno != EAGAIN)
		{
			if (errno == EBADF || errno == EINTR)
			{
				/* end of process */
				Main_Exit();
				return;
			}
			else
			{
				printf(Neuro_s("read buffer empty, errno set %d\n", errno));
			}
		}
	}
}
예제 #7
0
const std::string sha1head(const std::string& data, size_t head) {
    return sha1sum(data).substr(0, head);
}
예제 #8
0
파일: alignment.cpp 프로젝트: ktym/vg
const string hash_alignment(const Alignment& aln) {
    string data;
    aln.SerializeToString(&data);
    return sha1sum(data);
}
static int LoadPartitionContents(const char* filename, FileContents* file) {
    std::string copy(filename);
    std::vector<std::string> pieces = android::base::Split(copy, ":");
    if (pieces.size() < 4 || pieces.size() % 2 != 0) {
        printf("LoadPartitionContents called with bad filename (%s)\n", filename);
        return -1;
    }

    enum PartitionType type;
    if (pieces[0] == "MTD") {
        type = MTD;
    } else if (pieces[0] == "EMMC") {
        type = EMMC;
    } else {
        printf("LoadPartitionContents called with bad filename (%s)\n", filename);
        return -1;
    }
    const char* partition = pieces[1].c_str();

    size_t pairs = (pieces.size() - 2) / 2;    // # of (size, sha1) pairs in filename
    std::vector<size_t> index(pairs);
    std::vector<size_t> size(pairs);
    std::vector<std::string> sha1sum(pairs);

    for (size_t i = 0; i < pairs; ++i) {
        size[i] = strtol(pieces[i*2+2].c_str(), NULL, 10);
        if (size[i] == 0) {
            printf("LoadPartitionContents called with bad size (%s)\n", filename);
            return -1;
        }
        sha1sum[i] = pieces[i*2+3].c_str();
        index[i] = i;
    }

    // Sort the index[] array so it indexes the pairs in order of increasing size.
    sort(index.begin(), index.end(),
        [&](const size_t& i, const size_t& j) {
            return (size[i] < size[j]);
        }
    );

    MtdReadContext* ctx = NULL;
    FILE* dev = NULL;

    switch (type) {
        case MTD: {
            if (!mtd_partitions_scanned) {
                mtd_scan_partitions();
                mtd_partitions_scanned = true;
            }

            const MtdPartition* mtd = mtd_find_partition_by_name(partition);
            if (mtd == NULL) {
                printf("mtd partition \"%s\" not found (loading %s)\n", partition, filename);
                return -1;
            }

            ctx = mtd_read_partition(mtd);
            if (ctx == NULL) {
                printf("failed to initialize read of mtd partition \"%s\"\n", partition);
                return -1;
            }
            break;
        }

        case EMMC:
            dev = fopen(partition, "rb");
            if (dev == NULL) {
                printf("failed to open emmc partition \"%s\": %s\n", partition, strerror(errno));
                return -1;
            }
    }

    SHA_CTX sha_ctx;
    SHA_init(&sha_ctx);
    uint8_t parsed_sha[SHA_DIGEST_SIZE];

    // Allocate enough memory to hold the largest size.
    file->data = reinterpret_cast<unsigned char*>(malloc(size[index[pairs-1]]));
    char* p = (char*)file->data;
    file->size = 0;                // # bytes read so far
    bool found = false;

    for (size_t i = 0; i < pairs; ++i) {
        // Read enough additional bytes to get us up to the next size. (Again,
        // we're trying the possibilities in order of increasing size).
        size_t next = size[index[i]] - file->size;
        size_t read = 0;
        if (next > 0) {
            switch (type) {
                case MTD:
                    read = mtd_read_data(ctx, p, next);
                    break;

                case EMMC:
                    read = fread(p, 1, next, dev);
                    break;
            }
            if (next != read) {
                printf("short read (%zu bytes of %zu) for partition \"%s\"\n",
                       read, next, partition);
                free(file->data);
                file->data = NULL;
                return -1;
            }
            SHA_update(&sha_ctx, p, read);
            file->size += read;
        }

        // Duplicate the SHA context and finalize the duplicate so we can
        // check it against this pair's expected hash.
        SHA_CTX temp_ctx;
        memcpy(&temp_ctx, &sha_ctx, sizeof(SHA_CTX));
        const uint8_t* sha_so_far = SHA_final(&temp_ctx);

        if (ParseSha1(sha1sum[index[i]].c_str(), parsed_sha) != 0) {
            printf("failed to parse sha1 %s in %s\n", sha1sum[index[i]].c_str(), filename);
            free(file->data);
            file->data = NULL;
            return -1;
        }

        if (memcmp(sha_so_far, parsed_sha, SHA_DIGEST_SIZE) == 0) {
            // we have a match.  stop reading the partition; we'll return
            // the data we've read so far.
            printf("partition read matched size %zu sha %s\n",
                   size[index[i]], sha1sum[index[i]].c_str());
            found = true;
            break;
        }

        p += read;
    }

    switch (type) {
        case MTD:
            mtd_read_close(ctx);
            break;

        case EMMC:
            fclose(dev);
            break;
    }


    if (!found) {
        // Ran off the end of the list of (size,sha1) pairs without finding a match.
        printf("contents of partition \"%s\" didn't match %s\n", partition, filename);
        free(file->data);
        file->data = NULL;
        return -1;
    }

    const uint8_t* sha_final = SHA_final(&sha_ctx);
    for (size_t i = 0; i < SHA_DIGEST_SIZE; ++i) {
        file->sha1[i] = sha_final[i];
    }

    // Fake some stat() info.
    file->st.st_mode = 0644;
    file->st.st_uid = 0;
    file->st.st_gid = 0;

    return 0;
}