Example usage for java.security Identity getPublicKey

List of usage examples for java.security Identity getPublicKey

Introduction

In this page you can find the example usage for java.security Identity getPublicKey.

Prototype

public PublicKey getPublicKey() 

Source Link

Document

Returns this identity's public key.

Usage

From source file:ch.bfh.evoting.alljoyn.BusHandler.java

/**
 * Helper method extracting an identity from a received message
 * @param messageObject the original message received
 *//*from  ww w.j a v a2  s. com*/
private void extractIdentity(AllJoynMessage messageObject) {
    Log.d(TAG, "Exctracting identity " + messageObject.getMessage());

    //Get the name and its corresponding key key
    StringTokenizer tokenizer = new StringTokenizer(messageObject.getMessage(), MESSAGE_PARTS_SEPARATOR);
    if (tokenizer.countTokens() != 2) {
        //malformed string
        Log.d(TAG, "String was not composed of 2 parts");
        return;
    }

    String peerId = messageObject.getSender();
    //then peerName
    String peerName = (String) tokenizer.nextElement();
    //and finally peerKey
    String peerKey = (String) tokenizer.nextElement();

    Identity newIdentity = new Identity(peerName, messageAuthenticater.decodePublicKey(peerKey));

    //Check if identity is already known
    if (identityMap.containsKey(peerId)) {
        //if yes, check if the same identity as received before
        if (!identityMap.get(peerId).equals(newIdentity)) {
            //If not someone is trying to impersonate somebody else
            Intent intent = new Intent("attackDetected");
            intent.putExtra("type", 1);
            LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
            Log.e(TAG, "Two different data received for peer " + peerId);
            return;
        }
    } else {
        boolean verification = messageObject.verifyMessage(newIdentity.getPublicKey());
        if (verification) {
            //Save the new identity
            Log.d(TAG, "identity received " + newIdentity.getName());
            identityMap.put(peerId, newIdentity);
            this.sendMyIdentity();
            //Update the UI
            LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent("participantStateUpdate"));

            if (peerId.equals(signatureVerificationTask.getSender())) {
                verifySignatureSalt();
            }
        } else {
            Log.e(TAG, "Wrong signature for identiy message from " + peerId);
            return;
        }
    }
}