// ChangePin.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "sense4.h"
#include <stdio.h>
#include "AccKey.h"


int main(int argc, char* argv[])
{
	SENSE4_CONTEXT * pCtx = NULL;
	DWORD			dwRet;
	BYTE			pOldDevPin[24]={0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,
									0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,
									0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38};

	BYTE			pOldUserPin[8]={0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38};
	
	BYTE			pNewDevPin[24] ={0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,
									0x32,0x32,0x32,0x32,0x32,0x32,0x32,0x32,
									0x33,0x33,0x33,0x33,0x33,0x33,0x33,0x33};

	BYTE			pNewUserPin[8]={0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31};

	BYTE			pNewAuthenPin[16] ={0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,
										0x32,0x32,0x32,0x32,0x32,0x32,0x32,0x32};


	//Access key
	pCtx = AccessKey();
	if(pCtx == NULL)
	{
		printf("Access device failed.");
		return 0;
	}
	
	//Note:
	//1,Before modifying DEV PIN, you needn't verify PIN first.
	//2,The root directory, each child directory of local device and each module of net device have its own DEV PIN.
	dwRet=S4ChangePin(pCtx,pOldDevPin,24,pNewDevPin,24,S4_DEV_PIN);

	//Note:
	//1,Before modifying User PIN, you needn't verify any PIN first.
	//2,For local device, root directory and each child directory have its own user PIN.
	//For net device, ONLY root directory has its own user PIN, any child module has NOT user PIN.
	dwRet=S4ChangePin(pCtx,pOldUserPin,8,pNewUserPin,8,S4_USER_PIN);
	
	//Note:
	//1,Before modifying authenticate key, you must verify DEV PIN first.
	//2,Only root directory of net device has authentication key, any directory of local device or
	//child directory of net device have NOT authentication key.
	//3,the length of authentication key is 16 or 8.
	dwRet=S4VerifyPin(pCtx,pOldDevPin,24,S4_DEV_PIN);	

	//modify authentication key
	dwRet=S4ChangePin(pCtx,NULL,0,pNewAuthenPin,16,S4_AUTHEN_PIN);
	
	//close device
	S4Close(pCtx);

	//free memory
	free(pCtx);
	pCtx = NULL;
	return 0;
}
