Skip to content

Instantly share code, notes, and snippets.

@Jaymo
Jaymo / SealedBoxUtility.java
Created May 8, 2018 14:28
SealedBoxUtility
package org.timby.crypto;
import org.timby.util.TweetNaclFast;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import ove.crypto.digest.Blake2b;
/**
import android.content.Context;
import android.graphics.Typeface;
import android.support.v4.util.SimpleArrayMap;
public class TypefaceHelper {
private static final SimpleArrayMap<String, Typeface> cache = new SimpleArrayMap<>();
public static Typeface get(Context c, String name) {
synchronized (cache) {
public static Cipher createCipher(int mode,SecretKey key,String iv) throws Exception {
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", "BC");
cipher.init(
Cipher.DECRYPT_MODE,
new SecretKeySpec(key, "AES"),
new IvParameterSpec(Util.HexEncoder.toByte(iv))
);
private final static int DEFAULT_READ_WRITE_BLOCK_BUFFER_SIZE = 1024;
public static void applyCipher(String inFile, String outFile, Cipher cipher) throws Exception {
CipherInputStream in = new CipherInputStream(new FileInputStream(inFile), cipher);
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFile));
byte[] buffer = new byte[DEFAULT_READ_WRITE_BLOCK_BUFFER_SIZE];
int count = 0;
while ((count = in.read(buffer)) >= 0) {
private static final String RANDOM_ALGORITHM = "SHA1PRNG";
public static final int IV_LENGTH = 16;
private byte[] generateIv() throws NoSuchAlgorithmException, NoSuchProviderException {
SecureRandom random = SecureRandom.getInstance(RANDOM_ALGORITHM);
byte[] iv = new byte[IV_LENGTH];
random.nextBytes(iv);
return iv;
}
public static final int SALT_LENGTH = 256;
private static final String RANDOM_ALGORITHM = "SHA1PRNG";
public String generateSalt() throws CryptoException {
try {
SecureRandom random = SecureRandom.getInstance(RANDOM_ALGORITHM);
byte[] salt = new byte[SALT_LENGTH];
random.nextBytes(salt);
String saltHex = Util.HexEncoder.toHex(salt);
return saltHex;
public static final int PBE_ITERATION_COUNT = 10000; //Am not sure on this size and low end devices. I should test more to prevent lag
private static final String PBE_ALGORITHM = "PBKDF2WithHmacSHA1";
public static final String PROVIDER = "BC";
private static final String SECRET_KEY_ALGORITHM = "AES/GCM/NoPadding;
public static SecretKey getSecretKey(String password, String salt) throws CryptoException {
try {
PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), Util.HexEncoder.toByte(salt), PBE_ITERATION_COUNT, 256);
SecretKeyFactory factory = SecretKeyFactory.getInstance(PBE_ALGORITHM, PROVIDER);
SecretKey tmp = factory.generateSecret(pbeKeySpec);
@Jaymo
Jaymo / file.rb
Created August 9, 2014 21:02
Take a look at "PesaBox"
https://play.google.com/store/apps/details?id=com.myitprovider.pesabox
@Jaymo
Jaymo / volleyHenry.java
Created June 16, 2014 08:51
Volley: passing parameters in a string request
StringRequest loginReq = new StringRequest(Method.GET,
URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
respnse=response;
}
}, new Response.ErrorListener() {
@Jaymo
Jaymo / OkHttpStack.java
Last active August 29, 2015 14:02
A `HttpStack` implementation for Volley that uses OkHttp 2.0 as its transport.
package com.ujuzicode.repocode;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import com.android.volley.toolbox.HurlStack;
import com.squareup.okhttp.OkHttpClient;
/**