FreeBSD/contrib/ntp/ntpd/ntp_leapsec.c
/* [internal] parse a digest line to get the hash signature
 * The NIST code creating the hash writes them out as 5 hex integers
 * without leading zeros. This makes reading them back as hex-encoded
 * BLOB impossible, because there might be less than 40 hex digits.
 *
 * The solution is to read the values back as integers, and then do the
 * byte twiddle necessary to get it into an array of 20 chars. The
 * drawback is that it permits any acceptable number syntax provided by
 * 'scanf()' and 'strtoul()', including optional signs and '0x'
 * prefixes.
 */
[internal] parse a digest line to get the hash signature
The NIST code creating the hash writes them out as 5 hex integers
without leading zeros. This makes reading them back as hex-encoded
BLOB impossible, because there might be less than 40 hex digits.

The solution is to read the values back as integers, and then do the
byte twiddle necessary to get it into an array of 20 chars.
[ internal ] parse a digest line to get the hash signature The NIST code creating the hash writes them out as 5 hex integers without leading zeros . This makes reading them back as hex - encoded BLOB impossible because there might be less than 40 hex digits . The solution is to read the values back as integers and then do the byte twiddle necessary to get it into an array of 20 chars .
static int
do_leap_hash(
	sha1_digest * mac,
	char const  * cp )
{
	int wi, di, num, len;
	unsigned long tmp[5];
	memset(mac, 0, sizeof(*mac));
	num = sscanf(cp, " %lx %lx %lx %lx %lx%n",
		     &tmp[0], &tmp[1], &tmp[2], &tmp[3], &tmp[4],
		     &len);
	if (num != 5 || cp[len] > ' ')
		return FALSE;
	for (wi=0; wi < 5; ++wi)
		for (di=3; di >= 0; --di) {
			mac->hv[wi*4 + di] =
				(unsigned char)(tmp[wi] & 0x0FF);
			tmp[wi] >>= 8;
		}
	return TRUE;
}
static int do_leap_hash ( sha1_digest * mac , char const * cp ) { int wi , di , num , len ; unsigned long tmp [ 5 ] ; memset ( mac , 0 , sizeof ( * mac ) ) ; num = sscanf ( cp , " " , & tmp [ 0 ] , & tmp [ 1 ] , & tmp [ 2 ] , & tmp [ 3 ] , & tmp [ 4 ] , & len ) ; if ( num != 5 || cp [ len ] > ' ' ) return FALSE ; for ( wi = 0 ; wi < 5 ; ++ wi ) for ( di = 3 ; di >= 0 ; -- di ) { mac -> hv [ wi * 4 + di ] = ( unsigned char ) ( tmp [ wi ] & 0x0FF ) ; tmp [ wi ] >>= 8 ; } return TRUE ; }
[internal] parse a digest line to get the hash signature
 The NIST code creating the hash writes them out as 5 hex integers
 without leading zeros.
[ internal ] parse a digest line to get the hash signature The NIST code creating the hash writes them out as 5 hex integers without leading zeros .
/*BOOL*/ /* now do the byte twiddle */