public static byte[][] genKeyPairBytes(int keySize) throws NoSuchAlgorithmException, NoSuchProviderException { byte[][] keyPairBytes = new byte[2][]; KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA", "SunRsaSign"); gen.initialize(keySize, new SecureRandom()); KeyPair pair = gen.generateKeyPair(); keyPairBytes[0] = pair.getPrivate().getEncoded(); keyPairBytes[1] = pair.getPublic().getEncoded(); return keyPairBytes; }
private static boolean supportsClone(MessageDigest digest) { try { digest.clone(); return true; } catch (CloneNotSupportedException e) { return false; } }
MessageDigest md = MessageDigest.getInstance("MD5"); try (InputStream is = Files.newInputStream(Paths.get("file.txt")); DigestInputStream dis = new DigestInputStream(is, md)) { /* Read decorated stream (dis) to EOF as normal... */ } byte[] digest = md.digest();
import java.security.*; .. byte[] bytesOfMessage = yourString.getBytes("UTF-8"); MessageDigest md = MessageDigest.getInstance("MD5"); byte[] thedigest = md.digest(bytesOfMessage);
public static InetAddress[] allAddressesByName(final String hostname) throws UnknownHostException { try { return AccessController.doPrivileged(new PrivilegedExceptionAction<InetAddress[]>() { @Override public InetAddress[] run() throws UnknownHostException { return InetAddress.getAllByName(hostname); } }); } catch (PrivilegedActionException e) { throw (UnknownHostException) e.getCause(); } }
private KeyPair generateKeyPair() { try { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(keyAlgorithm); keyPairGenerator.initialize(keySize, new SecureRandom()); return keyPairGenerator.generateKeyPair(); } catch (GeneralSecurityException e) { throw new AssertionError(e); } } }
public static ProtectionDomain getProtectionDomain(final Class source) { if (source == null) { return null; } return (ProtectionDomain) AccessController.doPrivileged(new PrivilegedAction() { public Object run() { return source.getProtectionDomain(); } }); }
import java.security.SecureRandom; import java.math.BigInteger; public final class SessionIdentifierGenerator { private SecureRandom random = new SecureRandom(); public String nextSessionId() { return new BigInteger(130, random).toString(32); } }
/** * Return the security context for this bean factory. If a security manager * is set, interaction with the user code will be executed using the privileged * of the security context returned by this method. * @see AccessController#getContext() */ protected AccessControlContext getAccessControlContext() { return AccessController.getContext(); }
@Override public void checkPermission(Permission perm) { // Disallowing access to System#getenv means that our // ReadOnlySystemAttributesMap will come into play. if ("getenv.*".equals(perm.getName())) { throw new AccessControlException("Accessing the system environment is disallowed"); } } };
@Override public void checkPropertiesAccess() { throw new AccessControlException("Not Allowed"); } @Override
public static SocketChannel accept(final ServerSocketChannel serverSocketChannel) throws IOException { try { return AccessController.doPrivileged(new PrivilegedExceptionAction<SocketChannel>() { @Override public SocketChannel run() throws IOException { return serverSocketChannel.accept(); } }); } catch (PrivilegedActionException e) { throw (IOException) e.getCause(); } }
@Override public AccessControlContext getAccessControlContext() { return (this.acc != null ? this.acc : AccessController.getContext()); }
@Override public void checkPropertiesAccess() { // see http://download.oracle.com/javase/1.5.0/docs/api/java/lang/System.html#getProperties() throw new AccessControlException("Accessing the system properties is disallowed"); } @Override
public static InetAddress addressByName(final String hostname) throws UnknownHostException { try { return AccessController.doPrivileged(new PrivilegedExceptionAction<InetAddress>() { @Override public InetAddress run() throws UnknownHostException { return InetAddress.getByName(hostname); } }); } catch (PrivilegedActionException e) { throw (UnknownHostException) e.getCause(); } }
public static void bind(final SocketChannel socketChannel, final SocketAddress address) throws IOException { try { AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() { @Override public Void run() throws IOException { socketChannel.bind(address); return null; } }); } catch (PrivilegedActionException e) { throw (IOException) e.getCause(); } }
public static void bind(final DatagramChannel networkChannel, final SocketAddress address) throws IOException { try { AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() { @Override public Void run() throws IOException { networkChannel.bind(address); return null; } }); } catch (PrivilegedActionException e) { throw (IOException) e.getCause(); } }
public static boolean connect(final SocketChannel socketChannel, final SocketAddress remoteAddress) throws IOException { try { return AccessController.doPrivileged(new PrivilegedExceptionAction<Boolean>() { @Override public Boolean run() throws IOException { return socketChannel.connect(remoteAddress); } }); } catch (PrivilegedActionException e) { throw (IOException) e.getCause(); } }
public static byte[] hardwareAddressFromNetworkInterface(final NetworkInterface intf) throws SocketException { try { return AccessController.doPrivileged(new PrivilegedExceptionAction<byte[]>() { @Override public byte[] run() throws SocketException { return intf.getHardwareAddress(); } }); } catch (PrivilegedActionException e) { throw (SocketException) e.getCause(); } } }
public static void bind(final Socket socket, final SocketAddress bindpoint) throws IOException { try { AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() { @Override public Void run() throws IOException { socket.bind(bindpoint); return null; } }); } catch (PrivilegedActionException e) { throw (IOException) e.getCause(); } }