List of usage examples for javax.crypto SecretKey equals
public boolean equals(Object obj)
From source file:MainClass.java
public static void main(String args[]) throws Exception { KeyGenerator keyGen = KeyGenerator.getInstance("DES"); SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN"); random.setSeed(101L);/*from w w w .j ava 2 s . c om*/ keyGen.init(56, random); SecretKey sKey = keyGen.generateKey(); SecretKeyFactory kfactory = SecretKeyFactory.getInstance("DES"); DESKeySpec kspec = (DESKeySpec) kfactory.getKeySpec(sKey, DESKeySpec.class); System.out.println(sKey); FileOutputStream fos = new FileOutputStream("secretKeys"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(kspec.getKey()); FileInputStream fin = new FileInputStream("secretKeys"); ObjectInputStream ois = new ObjectInputStream(fin); byte[] kMaterial = (byte[]) ois.readObject(); DESKeySpec keyspec = new DESKeySpec(kMaterial); SecretKey newKey = kfactory.generateSecret(keyspec); System.out.println(newKey); System.out.println("Do the keys equal :" + newKey.equals(sKey)); }
From source file:Main.java
public static void main(String[] argv) throws Exception { // Generate a key KeyGenerator keyGen = KeyGenerator.getInstance("DESede"); SecretKey key = keyGen.generateKey(); // Get the bytes of the key byte[] keyBytes = key.getEncoded(); int numBytes = keyBytes.length; // The bytes can be converted back to a SecretKey SecretKey key2 = new SecretKeySpec(keyBytes, "DESede"); boolean b = key.equals(key2); // true }
From source file:com.alta189.deskbin.util.CryptUtils.java
public static SecretKey getSecretKey() { if (key != null) { return key; }// w ww . j a va2 s. c om SecretKey compare = null; if (!keyFile.exists()) { compare = getNewSecretKey(); if (!writeKey(compare)) { return null; } } if (loadKey()) { if (compare != null && !compare.equals(key)) { key = null; } return key; } return null; }
From source file:org.apache.hadoop.yarn.server.resourcemanager.security.TestApplicationTokens.java
/** * Validate master-key-roll-over and that tokens are usable even after * master-key-roll-over.//from www . ja v a 2 s .c om * * @throws Exception */ @Test public void testMasterKeyRollOver() throws Exception { Configuration config = new Configuration(); MyContainerManager containerManager = new MyContainerManager(); final MockRM rm = new MockRMWithAMS(config, containerManager); rm.start(); try { MockNM nm1 = rm.registerNode("localhost:1234", 5120); RMApp app = rm.submitApp(1024); nm1.nodeHeartbeat(true); int waitCount = 0; while (containerManager.amContainerEnv == null && waitCount++ < 20) { LOG.info("Waiting for AM Launch to happen.."); Thread.sleep(1000); } Assert.assertNotNull(containerManager.amContainerEnv); RMAppAttempt attempt = app.getCurrentAppAttempt(); ApplicationAttemptId applicationAttemptId = attempt.getAppAttemptId(); // Create a client to the RM. final Configuration conf = rm.getConfig(); final YarnRPC rpc = YarnRPC.create(conf); UserGroupInformation currentUser = UserGroupInformation .createRemoteUser(applicationAttemptId.toString()); String tokenURLEncodedStr = containerManager.amContainerEnv .get(ApplicationConstants.APPLICATION_MASTER_TOKEN_ENV_NAME); LOG.info("AppMasterToken is " + tokenURLEncodedStr); Token<? extends TokenIdentifier> token = new Token<TokenIdentifier>(); token.decodeFromUrlString(tokenURLEncodedStr); currentUser.addToken(token); AMRMProtocol rmClient = createRMClient(rm, conf, rpc, currentUser); RegisterApplicationMasterRequest request = Records.newRecord(RegisterApplicationMasterRequest.class); request.setApplicationAttemptId(applicationAttemptId); rmClient.registerApplicationMaster(request); // One allocate call. AllocateRequest allocateRequest = Records.newRecord(AllocateRequest.class); allocateRequest.setApplicationAttemptId(applicationAttemptId); Assert.assertFalse(rmClient.allocate(allocateRequest).getAMResponse().getReboot()); // Simulate a master-key-roll-over ApplicationTokenSecretManager appTokenSecretManager = rm.getRMContext() .getApplicationTokenSecretManager(); SecretKey oldKey = appTokenSecretManager.getMasterKey(); appTokenSecretManager.rollMasterKey(); SecretKey newKey = appTokenSecretManager.getMasterKey(); Assert.assertFalse("Master key should have changed!", oldKey.equals(newKey)); // Another allocate call. Should continue to work. rpc.stopProxy(rmClient, conf); // To avoid using cached client rmClient = createRMClient(rm, conf, rpc, currentUser); allocateRequest = Records.newRecord(AllocateRequest.class); allocateRequest.setApplicationAttemptId(applicationAttemptId); Assert.assertFalse(rmClient.allocate(allocateRequest).getAMResponse().getReboot()); } finally { rm.stop(); } }