Added PBKDF2 hash method

This commit is contained in:
Ziver Koc 2015-09-29 20:51:38 +00:00
parent 577aea998b
commit 6d1b8898c8
6 changed files with 124 additions and 31 deletions

BIN
ZUtil.jar Normal file

Binary file not shown.

View file

@ -25,6 +25,8 @@ package zutil;
import zutil.converters.Converter;
import javax.crypto.Mac;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.math.BigInteger;
@ -65,7 +67,7 @@ public class Hasher {
/**
* Returns the MD5 hash of the given object
*
* @param object is the String to hash
* @param str is the String to hash
* @return an String containing the hash
*/
public static String MD5(String str){
@ -175,6 +177,23 @@ public class Hasher {
}
return null;
}
public static String PBKDF2(String data, String salt, int iterations){
try {
PBEKeySpec spec = new PBEKeySpec(
data.toCharArray(),
salt.getBytes(),
iterations,
256);
SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
byte[] raw = f.generateSecret(spec).getEncoded();
return Converter.toHexString(raw);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Hashes the given String as UTF8

View file

@ -50,22 +50,7 @@ public class SSDPClient extends ThreadedUDPNetwork implements ThreadedUDPNetwork
private HashMap<String, SSDPServiceInfo> services_usn;
private SSDPServiceListener listener;
public static void main(String[] args) throws IOException{
System.out.println(LogUtil.getCalingClass());
LogUtil.setGlobalLevel(Level.FINEST);
SSDPClient ssdp = new SSDPClient();
//ssdp.requestService("upnp:rootdevice");
ssdp.requestService("zap:discover");
ssdp.start();
for(int i=0; true ;++i){
while( i==ssdp.getServicesCount("upnp:rootdevice") ){ try{Thread.sleep(100);}catch(Exception e){} }
logger.log(Level.FINEST, "************************" );
logger.log(Level.FINEST, ""+ssdp.getServices("upnp:rootdevice").get(i) );
}
}
/**
* Creates new instance of this class. An UDP

View file

@ -77,20 +77,6 @@ public class SSDPServer extends ThreadedUDPNetwork implements ThreadedUDPNetwork
private HashMap<String, SSDPServiceInfo> services;
public static void main(String[] args) throws IOException{
LogUtil.setGlobalLevel(Level.FINEST);
StandardSSDPInfo service = new StandardSSDPInfo();
service.setLocation("nowhere");
service.setST("zep:discover");
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Alias", "Desktop");
headers.put("PublicKey", "SuperDesktopKey");
service.setHeaders(headers);
SSDPServer ssdp = new SSDPServer();
ssdp.addService(service);
ssdp.start();
logger.info("SSDP Server running");
}
public SSDPServer() throws IOException{
super( null, SSDP_MULTICAST_ADDR, SSDP_PORT );

View file

@ -0,0 +1,49 @@
/*
* Copyright (c) 2015 Ziver
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package zutil.test;
import zutil.log.LogUtil;
import zutil.net.ssdp.SSDPClient;
import java.io.IOException;
import java.util.logging.Level;
/**
* Created by Ziver on 2015-09-29.
*/
public class SSDPClientTest {
public static void main(String[] args) throws IOException {
System.out.println(LogUtil.getCalingClass());
LogUtil.setGlobalLevel(Level.FINEST);
SSDPClient ssdp = new SSDPClient();
ssdp.requestService("upnp:rootdevice");
ssdp.start();
for(int i=0; true ;++i){
while( i==ssdp.getServicesCount("upnp:rootdevice") ){ try{Thread.sleep(100);}catch(Exception e){} }
System.out.println("************************" );
System.out.println("" + ssdp.getServices("upnp:rootdevice").get(i));
}
}
}

View file

@ -0,0 +1,54 @@
/*
* Copyright (c) 2015 Ziver
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package zutil.test;
import zutil.log.LogUtil;
import zutil.net.ssdp.SSDPServer;
import zutil.net.ssdp.StandardSSDPInfo;
import java.io.IOException;
import java.util.HashMap;
import java.util.logging.Level;
/**
* Created by Ziver on 2015-09-29.
*/
public class SSDPServerTest {
public static void main(String[] args) throws IOException {
LogUtil.setGlobalLevel(Level.FINEST);
StandardSSDPInfo service = new StandardSSDPInfo();
service.setLocation("nowhere");
service.setST("zep:discover");
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Alias", "Desktop");
headers.put("PublicKey", "SuperDesktopKey");
service.setHeaders(headers);
SSDPServer ssdp = new SSDPServer();
ssdp.addService(service);
ssdp.start();
System.out.println("SSDP Server running");
}
}