EMMA Coverage Report (generated Wed Aug 10 12:30:04 EDT 2011)
[all classes][org.jclouds.crypto]

COVERAGE SUMMARY FOR SOURCE FILE [SshKeys.java]

nameclass, %method, %block, %line, %
SshKeys.java100% (1/1)88%  (7/8)95%  (165/174)87%  (26/30)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class SshKeys100% (1/1)88%  (7/8)95%  (165/174)87%  (26/30)
SshKeys (): void 0%   (0/1)0%   (0/3)0%   (0/1)
generate (): Map 100% (1/1)40%  (4/10)25%  (1/4)
<static initializer> 100% (1/1)100% (48/48)100% (1/1)
encodeAsOpenSSH (RSAPublicKey): String 100% (1/1)100% (42/42)100% (11/11)
encodeAsPem (RSAPrivateKey): String 100% (1/1)100% (6/6)100% (1/1)
encodeUint32 (int, ByteArrayDataOutput): void 100% (1/1)100% (31/31)100% (5/5)
generate (KeyPairGenerator): Map 100% (1/1)100% (28/28)100% (5/5)
generateRsaKeyPair (KeyPairGenerator): KeyPair 100% (1/1)100% (6/6)100% (2/2)

1/**
2 *
3 * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
4 *
5 * ====================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ====================================================================
18 */
19package org.jclouds.crypto;
20 
21import java.math.BigInteger;
22import java.security.KeyPair;
23import java.security.KeyPairGenerator;
24import java.security.NoSuchAlgorithmException;
25import java.security.interfaces.RSAPrivateKey;
26import java.security.interfaces.RSAPublicKey;
27import java.util.Map;
28 
29import com.google.common.annotations.Beta;
30import com.google.common.base.Throwables;
31import com.google.common.collect.ImmutableMap;
32import com.google.common.collect.ImmutableMap.Builder;
33import com.google.common.io.ByteArrayDataOutput;
34import com.google.common.io.ByteStreams;
35 
36/**
37 * Creates OpenSSH RSA keypairs
38 * 
39 * @author Adrian Cole
40 * @see <a href=
41 *      "http://stackoverflow.com/questions/3706177/how-to-generate-ssh-compatible-id-rsa-pub-from-java"
42 *      />
43 */
44@Beta
45public class SshKeys {
46   // All data type encoding is defined in the section #5 of RFC #4251. string
47   // and mpint (multiple precision integer) types are encoded this way :
48   private static final byte[] sshrsa = new byte[] { 0, 0, 0, 7, 's', 's', 'h', '-', 'r', 's', 'a' };
49 
50   /**
51    * 
52    * @param used
53    *           to generate RSA key pairs
54    * @return new 2048 bit keyPair
55    * @see Crypto#rsaKeyPairGenerator()
56    */
57   public static KeyPair generateRsaKeyPair(KeyPairGenerator generator) {
58      generator.initialize(2048);
59      return generator.genKeyPair();
60   }
61 
62   /**
63    * return a "public" -> rsa public key, "private" -> its corresponding
64    * private key
65    */
66   public static Map<String, String> generate() {
67      try {
68         return generate(KeyPairGenerator.getInstance("RSA"));
69      } catch (NoSuchAlgorithmException e) {
70         Throwables.propagate(e);
71         return null;
72      }
73   }
74 
75   public static Map<String, String> generate(KeyPairGenerator generator) {
76      KeyPair pair = generateRsaKeyPair(generator);
77      Builder<String, String> builder = ImmutableMap.<String, String> builder();
78      builder.put("public", encodeAsOpenSSH(RSAPublicKey.class.cast(pair.getPublic())));
79      builder.put("private", encodeAsPem(RSAPrivateKey.class.cast(pair.getPrivate())));
80      return builder.build();
81   }
82 
83   public static String encodeAsOpenSSH(RSAPublicKey key) {
84      ByteArrayDataOutput out = ByteStreams.newDataOutput();
85      /* encode the "ssh-rsa" string */
86      out.write(sshrsa);
87      /* Encode the public exponent */
88      BigInteger e = key.getPublicExponent();
89      byte[] data = e.toByteArray();
90      encodeUint32(data.length, out);
91      out.write(data);
92      /* Encode the modulus */
93      BigInteger m = key.getModulus();
94      data = m.toByteArray();
95      encodeUint32(data.length, out);
96      out.write(data);
97      return "ssh-rsa " + CryptoStreams.base64(out.toByteArray());
98   }
99 
100   public static String encodeAsPem(RSAPrivateKey key) {
101      return Pems.pem(key.getEncoded(), Pems.PRIVATE_PKCS1_MARKER, 64);
102   }
103 
104   public static void encodeUint32(int value, ByteArrayDataOutput out) {
105      out.write((byte) ((value >>> 24) & 0xff));
106      out.write((byte) ((value >>> 16) & 0xff));
107      out.write((byte) ((value >>> 8) & 0xff));
108      out.write((byte) (value & 0xff));
109   }
110 
111}

[all classes][org.jclouds.crypto]
EMMA 2.0.5312 (C) Vladimir Roubtsov