⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sqtest.c

📁 一些加密解密的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
	squareHashUpdate (&ctxHash, text, SQUARE_BLOCKSIZE);
	squareHashFinal  (&ctxHash, digest);
	if (memcmp (digest, check, SQUARE_BLOCKSIZE) == 0) {
		squarePrintBlock (digest, SQUARE_BLOCKSIZE, "digest #1 (OK)");
	} else {
		squarePrintBlock (digest, SQUARE_BLOCKSIZE, "digest #1 (ERROR)\a");
		squarePrintBlock (check, SQUARE_BLOCKSIZE, "expected");
	}
	squareHash (text, SQUARE_BLOCKSIZE, digest);
	if (memcmp (digest, check, SQUARE_BLOCKSIZE) == 0) {
		squarePrintBlock (digest, SQUARE_BLOCKSIZE, "digest #2 (OK)");
	} else {
		squarePrintBlock (digest, SQUARE_BLOCKSIZE, "digest #2 (ERROR)\a");
		squarePrintBlock (check, SQUARE_BLOCKSIZE, "expected");
	}
	printf ("\n");
} /* squareTestHashing */


static void squareMeasureRawSpeed (byte *text)
{
	squareKeySchedule roundKeys_e, roundKeys_d;
	long n; clock_t elapsed; double sec;

	squareGenerateRoundKeys (key, roundKeys_e, roundKeys_d);

	printf ("Measuring raw encryption speed...");
	elapsed = -clock ();
	for (n = 64*TIMING_ITERATIONS; n > 0; n--) {
		squareEncrypt((word32 *)text, roundKeys_e);
	}
	elapsed += clock ();
	sec = elapsed ? (double) elapsed / CLOCKS_PER_SEC : 1.0;
	printf (" %.2f sec, %.1f K/sec.\n",
		sec, 16*64*TIMING_ITERATIONS/1024/sec);

	printf ("Measuring raw decryption speed...");
	elapsed = -clock ();
	for (n = 64*TIMING_ITERATIONS; n > 0; n--) {
		squareDecrypt((word32 *)text, roundKeys_d);   
	}
	elapsed += clock ();
	sec = elapsed ? (double) elapsed / CLOCKS_PER_SEC : 1.0;
	printf (" %.2f sec, %.1f K/sec.\n",
		sec, 16*64*TIMING_ITERATIONS/1024/sec);
} /* squareMeasureRawSpeed */


static void squareMeasureEcbSpeed (byte *text, unsigned length)
{
	squareEcbContext ctxEcb;
	long n; clock_t elapsed; double sec;

	squareEcbInit (&ctxEcb, key);

	printf ("Measuring ECB encryption speed...");
	elapsed = -clock ();
	for (n = TIMING_ITERATIONS; n > 0; n--) {
		squareEcbEncrypt (&ctxEcb, text, length);
	}
	elapsed += clock ();
	sec = elapsed ? (double) elapsed / CLOCKS_PER_SEC : 1.0;
	printf (" %.2f sec, %.1f K/sec.\n",
		sec, (float)length*TIMING_ITERATIONS/1024.0/sec);

	printf ("Measuring ECB decryption speed...");
	elapsed = -clock ();
	for (n = TIMING_ITERATIONS; n > 0; n--) {
		squareEcbDecrypt (&ctxEcb, text, length);
	}
	elapsed += clock ();
	sec = elapsed ? (double) elapsed / CLOCKS_PER_SEC : 1.0;
	printf (" %.2f sec, %.1f K/sec.\n",
		sec, (float)length*TIMING_ITERATIONS/1024.0/sec);

	squareEcbFinal (&ctxEcb);
} /* squareMeasureEcbSpeed */


static void squareMeasureCbcSpeed (byte *text, unsigned length)
{
	squareCbcContext ctxCbc;
	long n; clock_t elapsed; double sec;

	squareCbcInit (&ctxCbc, key);

	printf ("Measuring CBC encryption speed...");
	elapsed = -clock ();
	for (n = TIMING_ITERATIONS; n > 0; n--) {
		squareCbcEncrypt (&ctxCbc, text, length);
	}
	elapsed += clock ();
	sec = elapsed ? (double) elapsed / CLOCKS_PER_SEC : 1.0;
	printf (" %.2f sec, %.1f K/sec.\n",
		sec, (float)length*TIMING_ITERATIONS/1024.0/sec);

	printf ("Measuring CBC decryption speed...");
	elapsed = -clock ();
	for (n = TIMING_ITERATIONS; n > 0; n--) {
		squareCbcDecrypt (&ctxCbc, text, length);
	}
	elapsed += clock ();
	sec = elapsed ? (double) elapsed / CLOCKS_PER_SEC : 1.0;
	printf (" %.2f sec, %.1f K/sec.\n",
		sec, (float)length*TIMING_ITERATIONS/1024.0/sec);

	squareCbcFinal (&ctxCbc);
} /* squareMeasureCbcSpeed */


static void squareMeasureCtsSpeed (byte *text, unsigned length)
{
	squareCtsContext ctxCts;
	long n; clock_t elapsed; double sec;

	squareCtsInit (&ctxCts, key);

	printf ("Measuring CTS encryption speed...");
	elapsed = -clock ();
	for (n = TIMING_ITERATIONS; n > 0; n--) {
		squareCtsEncrypt (&ctxCts, text, length);
	}
	elapsed += clock ();
	sec = elapsed ? (double) elapsed / CLOCKS_PER_SEC : 1.0;
	printf (" %.2f sec, %.1f K/sec.\n",
		sec, (float)length*TIMING_ITERATIONS/1024.0/sec);

	printf ("Measuring CTS decryption speed...");
	elapsed = -clock ();
	for (n = TIMING_ITERATIONS; n > 0; n--) {
		squareCtsDecrypt (&ctxCts, text, length);
	}
	elapsed += clock ();
	sec = elapsed ? (double) elapsed / CLOCKS_PER_SEC : 1.0;
	printf (" %.2f sec, %.1f K/sec.\n",
		sec, (float)length*TIMING_ITERATIONS/1024.0/sec);

	squareCtsFinal (&ctxCts);
} /* squareMeasureCtsSpeed */


static void squareMeasureCfbSpeed (byte *text, unsigned length)
{
	squareCfbContext ctxCfb;
	long n; clock_t elapsed; double sec;

	squareCfbInit (&ctxCfb, key);

	printf ("Measuring CFB encryption speed...");
	elapsed = -clock ();
	for (n = TIMING_ITERATIONS; n > 0; n--) {
		squareCfbEncrypt (&ctxCfb, text, length);
	}
	elapsed += clock ();
	sec = elapsed ? (double) elapsed / CLOCKS_PER_SEC : 1.0;
	printf (" %.2f sec, %.1f K/sec.\n",
		sec, (float)length*TIMING_ITERATIONS/1024.0/sec);

	printf ("Measuring CFB decryption speed...");
	elapsed = -clock ();
	for (n = TIMING_ITERATIONS; n > 0; n--) {
		squareCfbDecrypt (&ctxCfb, text, length);
	}
	elapsed += clock ();
	sec = elapsed ? (double) elapsed / CLOCKS_PER_SEC : 1.0;
	printf (" %.2f sec, %.1f K/sec.\n",
		sec, (float)length*TIMING_ITERATIONS/1024.0/sec);

	squareCfbFinal (&ctxCfb);
} /* squareMeasureCfbSpeed */


static void squareMeasureOfbSpeed (byte *text, unsigned length)
{
	squareOfbContext ctxOfb;
	long n; clock_t elapsed; double sec;

	squareOfbInit (&ctxOfb, key);

	printf ("Measuring OFB encryption speed...");
	elapsed = -clock ();
	for (n = TIMING_ITERATIONS; n > 0; n--) {
		squareOfbEncrypt (&ctxOfb, text, length);
	}
	elapsed += clock ();
	sec = elapsed ? (double) elapsed / CLOCKS_PER_SEC : 1.0;
	printf (" %.2f sec, %.1f K/sec.\n",
		sec, (float)length*TIMING_ITERATIONS/1024.0/sec);

	printf ("Measuring OFB decryption speed...");
	elapsed = -clock ();
	for (n = TIMING_ITERATIONS; n > 0; n--) {
		squareOfbDecrypt (&ctxOfb, text, length);
	}
	elapsed += clock ();
	sec = elapsed ? (double) elapsed / CLOCKS_PER_SEC : 1.0;
	printf (" %.2f sec, %.1f K/sec.\n",
		sec, (float)length*TIMING_ITERATIONS/1024.0/sec);

	squareOfbFinal (&ctxOfb);
} /* squareMeasureOfbSpeed */


static void squareMeasureHashingSpeed (byte *text, unsigned length)
{
	squareHashContext ctxHash;
	squareBlock digest;
	long n; clock_t elapsed; double sec;

	printf ("Measuring hashing speed...");
	elapsed = -clock ();
	squareHashInit (&ctxHash);
	for (n = TIMING_ITERATIONS; n > 0; n--) {
		squareHashUpdate (&ctxHash, text, length);
	}
	squareHashFinal (&ctxHash, digest);
	elapsed += clock ();
	sec = elapsed ? (double) elapsed / CLOCKS_PER_SEC : 1.0;
	printf (" %.2f sec, %.1f K/sec.\n",
		sec, (float)length*TIMING_ITERATIONS/1024.0/sec);
} /* squareMeasureHashingSpeed */


int main (void)
{
	printf ("%s\n", squareBanner);
	printf ("Checking correctness...\n");

	squarePrintBlock (key, SQUARE_BLOCKSIZE, "user key");

	/* check raw encryption/decryption: */
	squareTestRaw ();

	/* check ECB mode: */
	squareTestEcb ();

	/* check CBC mode: */
	squareTestCbc (NULL);

	/* check CTS mode: */
	squareTestCts (NULL);

	/* check CFB mode: */
	squareTestCfb (NULL);

	/* check OFB mode: */
	squareTestOfb (NULL);

	/* check hashing: */
	squareTestHashing ();

	/* measure raw speed: */
	squareMeasureRawSpeed (data);

	/* measure ECB speed: */
	squareMeasureEcbSpeed (data, 1024);

	/* measure CBC speed: */
	squareMeasureCbcSpeed (data, 1024);

	/* measure CTS speed: */
	squareMeasureCtsSpeed (data, 1024);

	/* measure CFB speed: */
	squareMeasureCfbSpeed (data, 1024);

	/* measure OFB speed: */
	squareMeasureOfbSpeed (data, 1024);

	/* measure hashing speed: */
	squareMeasureHashingSpeed (data, 1024);

	return 0;
} /* main */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -