📄 hashtime.c
字号:
/* TradeClient <http://tradeclient.sourceforge.net> * $Id: hashtime.c,v 1.4 2001/03/20 22:19:33 ttabner Exp $ * * Copyright (C) 1999-2000 Bynari Inc. * Copyright (C) 2001 Project TradeClient * * GPL code donated by Greg Copeland <gtcopeland@earthlink.net> */#include <stdlib.h>#include <stdio.h>#include <time.h>#include "hashtime.h"#ifdef SOLARIS25#define snprintf g_snprintf#endif/* * Takes the current time and hashes it into an * array of 6 bytes. Notice that the output * values doesn't have to be portable, just * consistant. This funtion returns the * value of time_t that was used. */time_thashTime( unsigned char *paddedArray ){ time_t t ; unsigned char lowNibble ; unsigned char highNibble ; unsigned char timeArray[4] ; /* Get current time and take it apart */ t = time( NULL ) ; timeArray[0] = ((t >> 0) & 0xFF ) ; timeArray[1] = ((t >> 8) & 0xFF ) ; timeArray[2] = ((t >> 16) & 0xFF ) ; timeArray[3] = ((t >> 24) & 0xFF ) ; /* Now, assign everything out to our padded values */ highNibble = (timeArray[1] >> 4) & 0x0F ; lowNibble = (timeArray[1] >> 0) & 0x0F ; paddedArray[0] = ((lowNibble << 4)+(highNibble)) ; paddedArray[1] = (timeArray[0] ^ timeArray[1]); highNibble = (timeArray[0] >> 4) & 0x0F ; lowNibble = (timeArray[0] >> 0) & 0x0F ; paddedArray[2] = ((lowNibble << 4)+(highNibble)) ; highNibble = (timeArray[3] >> 4) & 0x0F ; lowNibble = (timeArray[3] >> 0) & 0x0F ; paddedArray[3] = ((lowNibble << 4)+(highNibble)) ; paddedArray[4] = (timeArray[2] ^ timeArray[3]); highNibble = (timeArray[2] >> 4) & 0x0F ; lowNibble = (timeArray[2] >> 0) & 0x0F ; paddedArray[5] = ((lowNibble << 4)+(highNibble)) ; return t ;}/* * Takes a previously hashed value from hashTime * and sets the value of the time_t that's passed * in. Note that it returns 1 when a given hash * can not be validated. */intunhashTime( unsigned char *input, time_t *t ){ int ret ; unsigned char lowNibble ; unsigned char highNibble ; unsigned char timeArray[4] ; ret = 0 ; highNibble = (input[2] >> 4) & 0x0F ; lowNibble = (input[2] >> 0) & 0x0F ; timeArray[0] = ((lowNibble << 4)+(highNibble)) ; highNibble = (input[0] >> 4) & 0x0F ; lowNibble = (input[0] >> 0) & 0x0F ; timeArray[1] = ((lowNibble << 4)+(highNibble)) ; highNibble = (input[5] >> 4) & 0x0F ; lowNibble = (input[5] >> 0) & 0x0F ; timeArray[2] = ((lowNibble << 4)+(highNibble)) ; highNibble = (input[3] >> 4) & 0x0F ; lowNibble = (input[3] >> 0) & 0x0F ; timeArray[3] = ((lowNibble << 4)+(highNibble)) ; /* Set the value so that the caller has it */ *t = *(time_t *)timeArray ; /* * Now, verify the checksums; if checksums fail, we return 1! * Otherwise, we return with 0. */ if( (input[1] != (timeArray[0] ^ timeArray[1])) || (input[4] != (timeArray[2] ^ timeArray[3])) ) ret = 1 ; /* Now, return if we have a good checksum or not */ return ret ;}/* * Converts a hash into a string. */voidhashTimeString( char *dest, const unsigned char *hash ){ if( dest && hash ) { snprintf( dest, 13, "%02X%02X%02X%02X%02X%02X", hash[0], hash[1], hash[2], hash[3], hash[4], hash[5] ) ; } return ;}/* * Converts a string representation of a hash into a hash. * This doesn't do anything right now. * FIXME!!! */voidstringHashTime( unsigned char *hash, char *string ){ return ;}#ifdef TESTintmain( int argc, char *argV[] ){ int ret ; time_t t1 ; time_t t2 ; unsigned char hash[6] ; char string[14] ; t1 = hashTime( (unsigned char *)&hash ) ; printf( "time_t used to generate hash = '%ld'.\n", t1 ) ; printf( "%02X %02X %02X %02X %02X %02X\n", hash[0], hash[1], hash[2], hash[3], hash[4], hash[5] ) ; hashTimeString( string, hash ) ; printf( "hashTimeString gave us '%s'.\n", string ) ; stringHashTime( hash, string ) ; printf( "After calling stringHashTime, we get: " ) ; printf( "%02X %02X %02X %02X %02X %02X\n", hash[0], hash[1], hash[2], hash[3], hash[4], hash[5] ) ; ret = unhashTime( hash, &t2 ) ; printf( "unhashTime returned (1 = bad, 0 = good): %d.\n", ret ) ; printf( "dehashed time value is '%ld'.\n", t2 ) ; if( t1 != t2 ) printf( "Something bad happened between hashing and dehashing!!!! Hash test FAILED!\n" ) ; else printf( "Hash/dehash test PASSED! Looks good!\n" ) ; return ret ;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -