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

📄 pgpkeyid.c

📁 PGP8.0源码 请认真阅读您的文件包然后写出其具体功能
💻 C
字号:
/*____________________________________________________________________________
        Copyright (C) 2002 PGP Corporation
        All rights reserved.

        $Id: pgpKeyID.c,v 1.14 2002/08/06 20:11:00 dallen Exp $
____________________________________________________________________________*/
#include "pgpConfig.h"

#include <string.h>
#include "pgpMem.h"
#include "pgpHex.h"
#include "pgpErrors.h"
#include "pgpContext.h"
#include "pgpUtilitiesPriv.h"

#include "pgpKeys.h"
#include "pgpKeyPriv.h"

enum
{
	kPGPKeyIDVersion	= 1
};

/* the non-opaque PGPKeyID */
struct PGPKeyIDPriv
{
	PGPByte		version;	/* Implies length */
	PGPByte		length;		/* Number of valid bytes */
	PGPByte		pkalg;
	PGPByte		reserved;
	PGPByte		bytes[ 32 ];
};


#define kMaxKeyBytes			8
/* 0x + str + NULL */
#define kMaxKeyStringLength		( 2 + ( kMaxKeyBytes * 2 ) + 1 )


	PGPBoolean
pgpKeyIDIsValid( PGPKeyID const * id )
{
	PGPKeyIDPriv const *	priv	= (PGPKeyIDPriv const *)id;
	
	pgpAssert( sizeof( PGPKeyID ) == sizeof( PGPKeyIDPriv ) );
	
	return( IsntNull( priv ) && priv->version == kPGPKeyIDVersion && 
		( priv->length == 4 || priv->length == 8 ) );
}
#define PGPValidateKeyID( id )	PGPValidateParam( pgpKeyIDIsValid( id ) )



	static void
sNewKeyID(
	PGPByte			pkalg,
	PGPUInt32		length,
	PGPByte const *	bytes,
	PGPKeyID *		outID )
{
	PGPKeyIDPriv *	priv	= (PGPKeyIDPriv *)outID;
	
	pgpAssert( length == 4 || length == 8 );
	priv->version	= kPGPKeyIDVersion;
	priv->length	= length;
	priv->pkalg	= pkalg;
	pgpCopyMemory( bytes, priv->bytes, length );
}



	PGPError
pgpNewKeyIDFromRawData(
	const void *	data,
	PGPByte			pkalg,
	PGPSize			dataSize,
	PGPKeyID *		outID )
{
	PGPError	err	= kPGPError_NoErr;
	
	PGPValidateParam( dataSize == 4 || dataSize == 8 );
	
	sNewKeyID( pkalg, dataSize, (PGPByte const *)data, outID );
	
	return( err );
}
				
				
/*____________________________________________________________________________
	Convert a string into a key id.
	
	string must be of the form:
		0xdddddddd
		0xdddddddddddddddd
		dddddddd
		dddddddddddddddd
	which represents either a 4 byte or 8 byte key id.
____________________________________________________________________________*/
	PGPError 
PGPNewKeyIDFromString(
	const char *	string,
	PGPPublicKeyAlgorithm pkalg,
	PGPKeyID *		outID )
{
	PGPError		err	= kPGPError_NoErr;
	PGPSize			stringLength;
	const char *	cur	= NULL;
	PGPByte *		outCur	= NULL;
	PGPByte			keyBytes[ kMaxKeyBytes ];
	PGPSize			inputRemaining;
	
	PGPValidatePtr( outID );
	pgpClearMemory( outID, sizeof( *outID ) );
	PGPValidatePtr( string );
	
	pgpEnterPGPErrorFunction();

	/* optionally starts with "0x" */
	stringLength	= strlen( string );
	if ( string[ 0 ] == '0' && string[ 1 ] == 'x' )
	{
		stringLength	-= 2;
		cur				= string + 2;
	}
	else
	{
		cur		= string;
	}
	
	/* two hex chars for each raw byte */
	PGPValidateParam( stringLength == 4 * 2 || stringLength == 8 * 2 );
	
	outCur	= &keyBytes[ 0 ];
	inputRemaining	= stringLength;
	while ( inputRemaining != 0 )
	{
		const char	char1	= *cur++;
		const char	char2	= *cur++;
		
		inputRemaining	-= 2;	/* we used 2 bytes */
		
		PGPValidateParam( pgpIsValidHexChar( char1 ) &&
			pgpIsValidHexChar( char2 ) );
		
		*outCur++	= (pgpHexCharToNibble( char1 ) << 4 ) |
						pgpHexCharToNibble( char2 );
	}
	
	sNewKeyID( (PGPByte)pkalg, stringLength / 2, keyBytes, outID );
	return( err );
}

	PGPError 
PGPNewKeyID(
	const PGPByte 			*keyIDBytes,
	PGPSize 				numKeyIDBytes,
	PGPPublicKeyAlgorithm 	pkalg,
	PGPKeyID 				*outID )
{
	PGPError		err	= kPGPError_NoErr;

	PGPValidatePtr( outID );
	pgpClearMemory( outID, sizeof( *outID ) );
	PGPValidatePtr( keyIDBytes );
	PGPValidateParam( numKeyIDBytes == 4 || numKeyIDBytes == 8 );
	
	pgpEnterPGPErrorFunction();

	sNewKeyID( (PGPByte)pkalg, numKeyIDBytes, keyIDBytes, outID );
	return( err );
}

	PGPError 
PGPGetKeyIDAlgorithm(
	const PGPKeyID 			*keyID,
	PGPPublicKeyAlgorithm 	*pkalg )
{
	PGPError		err		= kPGPError_NoErr;
	PGPKeyIDPriv *	priv	= (PGPKeyIDPriv *)keyID;
	
	PGPValidatePtr( pkalg );
	PGPValidateKeyID( keyID );

	pgpEnterPGPErrorFunction();

	*pkalg = (PGPPublicKeyAlgorithm) priv->pkalg;
	
	return( err );
}

		
	PGPError
PGPGetKeyID(
	PGPKeyDBObjRef	key,
	PGPKeyID *		outID )
{
	PGPError			err	= kPGPError_NoErr;
	
	PGPValidatePtr( outID );
	pgpClearMemory( outID, sizeof( *outID ) );
	PGPValidateKeyDBObj( key );
	
	if( !OBJISKEY( key ) )
		return kPGPError_BadParams;

	pgpEnterPGPErrorFunction();

	pgpKeyID8( key, NULL, outID );
	
	return( err );
}

	PGPByte const *
pgpGetKeyIDBytes( PGPKeyID const *keyID )
{
	PGPKeyIDPriv *	priv	= (PGPKeyIDPriv *)keyID;
	
	return( &priv->bytes[ 0 ] );
}


	PGPError
PGPGetKeyIDString(
	PGPKeyID const *	keyID,
	PGPKeyIDStringType	type,
	char				outString[ 128 ] )
{
	PGPError		err	= kPGPError_NoErr;
	PGPKeyIDPriv *	priv	= (PGPKeyIDPriv *)keyID;
	
	PGPValidatePtr( outString );
	pgpClearMemory( outString, 128 );
	PGPValidateKeyID( keyID );
	PGPValidateParam( type == kPGPKeyIDString_Abbreviated ||
		type == kPGPKeyIDString_Full );
	
	pgpEnterPGPErrorFunction();

	if ( type == kPGPKeyIDString_Full || priv->length == 4 )
	{
		/* all the bytes */
		pgpBytesToHex( priv->bytes, priv->length, TRUE, outString );
	}
	else
	{
		/* abbreviated version */
		pgpAssert( priv->length == 8 );
		pgpBytesToHex( priv->bytes + 4, 4, TRUE, outString );
	}
	
	return( err );
}




/*____________________________________________________________________________
	Compare two key IDs.  Return 0 if they are the same, -1 if key1 < key2,
	1 if key1 > key2. 
____________________________________________________________________________*/
	PGPInt32 
PGPCompareKeyIDs(
	PGPKeyID const *	key1,
	PGPKeyID const *	key2)
{
	PGPKeyIDPriv const *	priv1	= (PGPKeyIDPriv *)key1;
	PGPKeyIDPriv const *	priv2	= (PGPKeyIDPriv *)key2;
	
	pgpEnterZeroFunction();

	pgpAssert( pgpKeyIDIsValid( key1 ) );
	pgpAssert( pgpKeyIDIsValid( key2 ) );
	
	if ( (! pgpKeyIDIsValid( key1 )) ||
		( ! pgpKeyIDIsValid( key2 )) )
		return( 0 );
	
	if ( priv1->length == priv2->length )
	{
		PGPSize		numBytes	= priv1->length;
		PGPUInt32	idx;
		
		/* compare as if first byte is high order byte */
		for( idx = 0; idx < numBytes; ++idx )
		{
			if ( priv1->bytes[ idx ] < priv2->bytes[ idx ] )
				return( -1 );
			if ( priv1->bytes[ idx ] > priv2->bytes[ idx ] )
				return( 1 );
		}
	}
	else
	{
		/* unequal sizes 4 vs 8 bytes */
		PGPByte const *	ptr1;
		PGPByte const *	ptr2;
		PGPBoolean		priv1Is8Byte	= priv1->length == 8;
		
		ptr1	= &priv1->bytes[ 0 ];
		ptr2	= &priv2->bytes[ 0 ];
		if ( priv1Is8Byte )
			ptr1	+= 4;	/* use low order 4 bytes */
		else
			ptr2	+= 4;	/* use low order 4 bytes */
		
		/* if not the same, arbitrarily say 4 byte ids are < 8 byte IDs */
		if ( ! pgpMemoryEqual( ptr1, ptr2, 4 ) )
			return( priv1Is8Byte ? 1 : -1 );
	}
	
	if( priv1->pkalg != (PGPByte)kPGPPublicKeyAlgorithm_Invalid &&
		priv2->pkalg != (PGPByte)kPGPPublicKeyAlgorithm_Invalid &&
		priv1->pkalg != priv2->pkalg )
	{
		return (priv1->pkalg > priv2->pkalg) ? 1 : -1;
	}

	return( 0 );
}



	PGPBoolean
pgpKeyIDsEqual(
	PGPKeyID const *	key1,
	PGPKeyID const *	key2)
{
	return( PGPCompareKeyIDs( key1, key2 ) == 0 );
}


	PGPUInt32
pgpKeyIDLength( PGPKeyID const * key1 )
{
	pgpAssert( pgpKeyIDIsValid( key1 ) );
	return ((PGPKeyIDPriv *)key1)->length;
}



/*__Editor_settings____

	Local Variables:
	tab-width: 4
	End:
	vi: ts=4 sw=4
	vim: si
_____________________*/

⌨️ 快捷键说明

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