@Test
  public void testApply() {
    final Crypto crypto = createMock(Crypto.class);
    KeyPairGenerator rsaKeyPairGenerator = createMock(KeyPairGenerator.class);
    final SecureRandom secureRandom = createMock(SecureRandom.class);

    expect(crypto.rsaKeyPairGenerator()).andReturn(rsaKeyPairGenerator);
    rsaKeyPairGenerator.initialize(2048, secureRandom);
    expect(rsaKeyPairGenerator.genKeyPair()).andReturn(keyPair);

    replay(crypto, rsaKeyPairGenerator, secureRandom);

    RsaSshKeyPairGenerator supplier =
        Guice.createInjector(
                new AbstractModule() {
                  protected void configure() {
                    bind(Crypto.class).toInstance(crypto);
                    bind(SecureRandom.class).toInstance(secureRandom);
                  }
                })
            .getInstance(RsaSshKeyPairGenerator.class);

    assertEquals(
        supplier.get(),
        ImmutableMap.of(
            "public", openSshKey, "private", PRIVATE_KEY.replaceAll("\n", lineSeparator)));

    verify(crypto, rsaKeyPairGenerator, secureRandom);
  }
 private byte[] hmacSha256(byte[] key, String s) {
   try {
     Mac hmacSHA256 = crypto.hmacSHA256(key);
     return hmacSHA256.doFinal(s.getBytes());
   } catch (Exception e) {
     throw new HttpException("Error signing request", e);
   }
 }
Пример #3
0
 public String signString(String toSign) {
   String signature;
   try {
     signature =
         CryptoStreams.base64(CryptoStreams.mac(InputSuppliers.of(toSign), crypto.hmacSHA1(key)));
   } catch (Exception e) {
     throw new HttpException("error signing request", e);
   }
   return signature;
 }
  /**
   * Calculates the object MD5 and returns it as eTag
   *
   * @param object
   * @return
   */
  private String getEtag(Blob object) {
    try {
      Payloads.calculateMD5(object, crypto.md5());
    } catch (IOException ex) {
      logger.error(
          ex,
          "An error occurred calculating MD5 for object with name %s.",
          object.getMetadata().getName());
      Throwables.propagate(ex);
    }

    String eTag = CryptoStreams.hex(object.getPayload().getContentMetadata().getContentMD5());
    return eTag;
  }
 @Override
 public X509Certificate deserialize(
     JsonElement json, Type typeOfT, JsonDeserializationContext context)
     throws JsonParseException {
   String keyText = json.getAsString().replaceAll("\\n", "\n");
   try {
     return Pems.x509Certificate(
         ByteSource.wrap(keyText.getBytes(Charsets.UTF_8)), crypto.certFactory());
   } catch (UnsupportedEncodingException e) {
     Throwables.propagate(e);
     return null;
   } catch (IOException e) {
     Throwables.propagate(e);
     return null;
   } catch (CertificateException e) {
     Throwables.propagate(e);
     return null;
   }
 }
 @Override
 public PublicKey deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
     throws JsonParseException {
   String keyText = json.getAsString().replaceAll("\\n", "\n");
   try {
     return crypto
         .rsaKeyFactory()
         .generatePublic(Pems.publicKeySpec(ByteSource.wrap(keyText.getBytes(Charsets.UTF_8))));
   } catch (UnsupportedEncodingException e) {
     Throwables.propagate(e);
     return null;
   } catch (InvalidKeySpecException e) {
     Throwables.propagate(e);
     return null;
   } catch (IOException e) {
     Throwables.propagate(e);
     return null;
   }
 }