示例#1
0
        public override byte[] GetKey(KeyProviderQueryContext ctx)
        {
            byte[] key = null;

            try
            {
                KeePassRFIDConfig rfidConfig = KeePassRFIDConfig.GetFromCurrentSession();
                ChipAction(new Action <Chip>(delegate(Chip chip)
                {
                    if (rfidConfig.KeyType == KeyType.NFC)
                    {
                        // Only tag type 4 supported for now.
                        NFCTagCardService nfcsvc = chip.getService(CardServiceType.CST_NFC_TAG) as NFCTagCardService;
                        if (nfcsvc == null)
                        {
                            throw new KeePassRFIDException(Properties.Resources.UnsupportedNFCTag);
                        }

                        NdefMessage msg = nfcsvc.readNDEF();
                        if (msg.getRecordCount() > 0)
                        {
                            NdefRecordCollection records = msg.getRecords();
                            if (records.Count > 0)
                            {
                                // Always use first record only
                                NdefRecord record = records[0];
                                // Don't care about payload type, use whole payload as the key
                                UCharCollection payload = record.getPayload();
                                if (payload.Count > 0)
                                {
                                    key = payload.ToArray();
                                }
                            }
                        }
                    }
                    else
                    {
                        UCharCollection csn = chip.getChipIdentifier();
                        if (csn.Count > 0)
                        {
                            key = csn.ToArray();
                        }
                    }
                }), rfidConfig);
            }
            catch (KeePassRFIDException ex)
            {
                MessageBox.Show(ex.Message, Properties.Resources.PluginError, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return(key);
        }