📄 shaibuttonuser33.java
字号:
* @param offset the offset into fullBindCode where copying should begin. */ public void getFullBindCode(byte[] l_fullBindCode, int offset) { System.arraycopy(this.fullBindCode,0, l_fullBindCode,offset, 15); /*System.arraycopy(ffBlock, 0, fullBindCode, offset, 15); fullBindCode[offset+4] = (byte)((0x40)|(byte)this.accountPageNumber); System.arraycopy(this.address, 0, fullBindCode, offset+5, 7);*/ } /** * <P>Returns a byte representing the appropriate authorization command * for the coprocessor to use to authenticate this user. For a DS1961S, * the authentication command is AUTH_HOST.</P> * * @return byte indicating appropriate command for authenticating user * */ public byte getAuthorizationCommand() { return OneWireContainer18.AUTH_HOST; } private byte[] writeAccountData_copyAuth = new byte[32]; private byte[] writeAccountData_scratchpad = new byte[32]; private byte[] writeAccountData_pageData = new byte[32]; /** * <P>Writes the account data to the SHAiButton. First, this function * asserts that the account page number is known. The account data is * copied from dataBuffer starting at the offset. If there are less * than 32 bytes available to copy, this function only copies the bytes * that are available.</P> * * <P>Note that for the DS1961S user button, an authorization MAC must * be generated for the copy-scratchpad command. Since the scratchpad * is only 8 bytes long, this must be done 4 times to write a page of * data. So, this function only writes (in 8 byte blocks) the bytes * that have changed.</P> * * @param dataBuffer the buffer to copy the account data from * @param offset the index into the buffer where copying should begin * @return whether or not the data write succeeded * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from a 1-Wire device. This could be * caused by a physical interruption in the 1-Wire Network due to * shorts or a newly arriving 1-Wire device issuing a 'presence pulse'. * @throws OneWireException on a communication or setup error with the 1-Wire * adapter */ public synchronized boolean writeAccountData(byte[] dataBuffer, int offset) throws OneWireException, OneWireIOException { //local vars. OneWireContainer33 ibcL = this.ibc33; byte[] copyAuth = this.writeAccountData_copyAuth; byte[] scratchpad = this.writeAccountData_scratchpad; byte[] pageData = this.writeAccountData_pageData; byte[] fullBindCode = this.fullBindCode; SHAiButtonCopr coprL = this.copr; //make sure account info is properly setup if(!checkAccountPageInfo(ibcL)) return false; //if the part is being initialized, the container class "knows" //the secret already. no need for a coprocessor. if(ibcL.isContainerSecretSet()) { //use container to write the data page, since it knows the secret ibcL.writeDataPage(this.accountPageNumber, dataBuffer); //update the data cache System.arraycopy(dataBuffer, offset, this.accountData, 0, 32); } else { //since the container's secret is not set, we have to use the //coprocessor for generating the copy scratchpad authorization. if(coprL==null) { throw new OneWireException("No Write Authorization Coprocessor Available!"); } //copy the data cache into a working page; System.arraycopy(this.accountData, 0, pageData, 0, 32); //takes four write/copies to actually write the data page. for(int i=24; i>=0; i-=8) { int index = offset + i; //only perform any action if the data needs to be updated if( (dataBuffer[index ] != accountData[i ]) || (dataBuffer[index+1] != accountData[i+1]) || (dataBuffer[index+2] != accountData[i+2]) || (dataBuffer[index+3] != accountData[i+3]) || (dataBuffer[index+4] != accountData[i+4]) || (dataBuffer[index+5] != accountData[i+5]) || (dataBuffer[index+6] != accountData[i+6]) || (dataBuffer[index+7] != accountData[i+7]) ) { //format the working page for generating the //appropriate copy authorization mac pageData[28] = dataBuffer[index]; pageData[29] = dataBuffer[index+1]; pageData[30] = dataBuffer[index+2]; pageData[31] = dataBuffer[index+3]; //format the scratchpad for generating the //appropriate copy authorization mac scratchpad[8] = dataBuffer[index+4]; scratchpad[9] = dataBuffer[index+5]; scratchpad[10] = dataBuffer[index+6]; scratchpad[11] = dataBuffer[index+7]; //add in the page num and address System.arraycopy(this.fullBindCode, 4, scratchpad, 12, 11); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ if(DEBUG) { IOHelper.writeLine("------------------------------------------------------------"); IOHelper.writeLine("SHAiButtonUser33 - writeAccountData loop "); IOHelper.writeLine("current account data state: "); IOHelper.writeBytesHex(this.accountData); IOHelper.writeLine("current byte block: " + i); IOHelper.writeLine("New bytes for block: "); IOHelper.writeBytesHex(dataBuffer,index,8); IOHelper.writeLine("------------------------------------------------------------"); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ //get the copy authorization mac coprL.createDataSignatureAuth(pageData, scratchpad, copyAuth, 0, fullBindCode); //only need to pass it to coprocessor once fullBindCode = null; //write 8 bytes of data to the DS1961S scratchpad if(!ibcL.writeScratchpad(this.accountPageNumber, i, dataBuffer, index, 8)) //operation failed return false; //copy scratchpad to page if(!ibcL.copyScratchpad(this.accountPageNumber, i, copyAuth, 0)) //operation failed return false; //update cache of account data System.arraycopy(dataBuffer, index, this.accountData, i, 8); //update our working copy of the account data System.arraycopy(this.accountData, 0, pageData, 0, 32); } } } return true; } /** * <P>Reads the account data off the SHAiButton using a standard READ * command. First, this function asserts that the account page number is * known as well as the length of the account file. The 32 byte account * data page is copied into dataBuffer starting at the given offset.</P> * * @param dataBuffer the buffer to copy the account data into * @param offset the index into the buffer where copying should begin * @return whether or not the read was successful */ public synchronized boolean readAccountData(byte[] dataBuffer, int offset) throws OneWireException, OneWireIOException { //init local vars OneWireContainer33 ibcL = this.ibc33; //make sure account info is properly setup if(!checkAccountPageInfo(ibcL)) { return false; } //if the cache is empty if(this.accountData[0]==0) { //read directly into local cache ibcL.readMemoryPage(this.accountPageNumber, this.accountData, 0); } //copy from cache into output buffer System.arraycopy(this.accountData, 0, dataBuffer, offset, 32); //had to work, right? return true; } /** * <P>Reads the account data off the SHAiButton using a READ_AUTHENTICATE * command. First, this function asserts that the account page number is * known as well as the length of the account file. Then it copies the * 3 byte challenge to the scratchpad before sending the command for * READ_AUTHENTICATE. The 32 byte account data page is copied into * dataBuffer starting at dataStart.</P> * * <P>In addition to the account data, this function also returns a * calculated MAC. The MAC requires 20 bytes after the start index. * The return value is the write cycle counter value for the account * data page<p> * * @param chlg the buffer containing a 3-byte random challenge. * @param chlgStart the index into the buffer where the 3 byte * challenge begins. * @param dataBuffer the buffer to copy the account data into * @param dataStart the index into the buffer where copying should begin * @param mac the buffer to copy the resulting Message Authentication Code * @param macStart the index into the mac buffer to start copying * * @return the value of the write cycle counter for the page */ public synchronized int readAccountData(byte[] chlg, int chlgStart, byte[] dataBuffer, int dataStart, byte[] mac, int macStart) throws OneWireException, OneWireIOException { //init local variables OneWireContainer33 ibcL = this.ibc33; //make sure account info is properly setup if(this.accountPageNumber<0) { //user not setup throw new OneWireException("SHAiButtonUser Not Properly Initialized"); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ if(DEBUG) { // for debug, lets use a constant challenge chlg[0] = 0x00; chlg[1] = 0x00; chlg[2] = 0x00; } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ //container keeps challenge as a member of the class ibcL.setChallenge(chlg, chlgStart); //performs the read authenticated page, to answer the challenge if(ibcL.readAuthenticatedPage(this.accountPageNumber, dataBuffer, dataStart, mac, macStart)) { //copy from outputbuffer into cache System.arraycopy(dataBuffer, dataStart, this.accountData, 0, 32); //has no write cycle counter return -1; } else { throw new OneWireException("SHAiButtonUser ReadAuthenticatedPage Failed"); } } /** * Refreshes eeprom SHA devices in case of weakly-programmed bits on * the account page. * * @return true if the refresh was successful */ public boolean refreshDevice() throws OneWireException,OneWireIOException { return ibc33.refreshPage(this.accountPageNumber); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -