Reading an X509 certificate in Java can be done using the following code. I used it specifically for obtaining a reference to the Apple WWDR certificate, but generally speaking, this will work for reading any certificate in X509 format.
import java.security.cert.*;
import java.io.*;
import org.apache.commons.io.IOUtils;
import java.security.NoSuchProviderException;
import java.security.NoSuchAlgorithmException;
import java.security.KeyStoreException;
public static X509Certificate readWWDRCertificate(String keyFile) throws IOException, KeyStoreException, NoSuchProviderException, NoSuchAlgorithmException, CertificateException
{
FileInputStream fis = null;
ByteArrayInputStream bais = null;
try
{
// use FileInputStream to read the file
fis = new FileInputStream(keyFile);
// read the bytes
byte value[] = new byte[fis.available()];
fis.read(value);
bais = new ByteArrayInputStream(value);
// get X509 certificate factory
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
// certificate factory can now create the certificate
return (X509Certificate)certFactory.generateCertificate(bais);
}
finally
{
IOUtils.closeQuietly(fis);
IOUtils.closeQuietly(bais);
}
}
The parameter you pass in should be the absolute location of the certificate file. In my case, my String parameter is something like "C:\\cert\AppleWWDRCA.cer".
Points of interest, usually heavily influenced by technology, gadgets and programming.
Showing posts with label apple. Show all posts
Showing posts with label apple. Show all posts
Friday, 14 September 2012
Thursday, 14 June 2012
Create JSON manifest file for directory
The following bash shell script was created for a very specific purpose; however due to licensing agreements the exact purpose cannot be disclosed as of yet. However, the techniques used here might prove useful to others striving to solve similar problems to that which I have just solved.
This script will inspect a directory of your choosing, and for each file in the directory, calculate the SHA-1 hash. A JSON file will then be created called "manifest.json" which lists each file along with its corresponding hash.
Usage: (Created and tested on a Mac)
From a Terminal prompt, enter:
Script
Download the script from ManifestMaker, or see below for the source code. Remember to make the script executable.
#!/bin/bash
#script which generates a JSON file, which has as keys the filenames of all files in the given directory, and as values the SHA1 digest of the corresponding files
#check number of arguments is as expected
if [ $# -ne 1 ]
then
echo "Need to provide directory on which to operate"
exit 1
fi
src="$1"
#check directory exists
if [ ! -d $src ]
then
echo "Directory $src does not exist"
exit 2
fi
#change to specified directory
cd $src
#declare target output file
out="manifest.json"
#clean up first
rm $out
touch $out
#find the number of files - needed to know whether to put a "," at the end of each line or not
numFiles=$(ls | wc -l)
#start printing to the file
echo "{" >> $out
index=1
for file in *
do
#check if comma is needed at the end of the line
if [ "$index" -lt "$numFiles" ]
then
endLine=","
else
endLine=""
fi
#gives some unfriendly output formatting
digestFull=$(openssl sha1 $file)
#tidy up formatting to get actual value
digest=${digestFull#*= }
#print line to file
echo " \"$file\" : \"$digest\"$endLine" >> $out
#increment our index
index=$[$index+1]
done
echo "}" >> $out
This script will inspect a directory of your choosing, and for each file in the directory, calculate the SHA-1 hash. A JSON file will then be created called "manifest.json" which lists each file along with its corresponding hash.
Usage: (Created and tested on a Mac)
From a Terminal prompt, enter:
bash manifestMaker.sh dirwhere dir is the directory for which you want to create a manifest.
Script
Download the script from ManifestMaker, or see below for the source code. Remember to make the script executable.
#!/bin/bash
#script which generates a JSON file, which has as keys the filenames of all files in the given directory, and as values the SHA1 digest of the corresponding files
#check number of arguments is as expected
if [ $# -ne 1 ]
then
echo "Need to provide directory on which to operate"
exit 1
fi
src="$1"
#check directory exists
if [ ! -d $src ]
then
echo "Directory $src does not exist"
exit 2
fi
#change to specified directory
cd $src
#declare target output file
out="manifest.json"
#clean up first
rm $out
touch $out
#find the number of files - needed to know whether to put a "," at the end of each line or not
numFiles=$(ls | wc -l)
#start printing to the file
echo "{" >> $out
index=1
for file in *
do
#check if comma is needed at the end of the line
if [ "$index" -lt "$numFiles" ]
then
endLine=","
else
endLine=""
fi
#gives some unfriendly output formatting
digestFull=$(openssl sha1 $file)
#tidy up formatting to get actual value
digest=${digestFull#*= }
#print line to file
echo " \"$file\" : \"$digest\"$endLine" >> $out
#increment our index
index=$[$index+1]
done
echo "}" >> $out
Monday, 24 October 2011
Custom HTTP Headers in iPhone UIWebView
If you've done much coding for iPhones, you'll likely have come across the UIWebView class. It is what allows you to use an embeddable web browser component, within your app. It's a useful component that lets you view web pages straight from within your app, using the very capable rendering capabilities granted to the iPhone's full Safari browser.
The problem I faced today, however, was that I needed to customise some of the HTTP headers that are sent to servers, when using the UIWebView component. Turns out that this is possible, although not as straight forward as I first expected. Why might you want to do this? Well you might want to alter the HTTP User Agent string, add some custom headers that your server requires, alter the language supported by the Accept header or you might well just be really curious.
Read the solution after the break.
Labels:
apple,
custom headers,
http,
http header,
ios,
iphone,
iphone development,
NSMutableURLRequest,
NSURLRequest,
obj-c,
objective c,
programming,
sdk,
uiwebview,
uiwebviewdelegate,
user agent
Subscribe to:
Posts (Atom)