-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#166: handle stdin properly with HTTP
- Loading branch information
Showing
8 changed files
with
214 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...nkedin.glu.utils/src/main/groovy/org/linkedin/glu/groovy/utils/io/GluGroovyIOUtils.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright (c) 2012 Yan Pujante | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package org.linkedin.glu.groovy.utils.io | ||
|
||
import org.linkedin.groovy.util.io.GroovyIOUtils | ||
|
||
import javax.crypto.Cipher | ||
import javax.crypto.CipherInputStream | ||
import javax.crypto.CipherOutputStream | ||
import javax.crypto.spec.SecretKeySpec | ||
|
||
/** | ||
* @author yan@pongasoft.com */ | ||
public class GluGroovyIOUtils extends GroovyIOUtils | ||
{ | ||
static InputStream decryptStream(String password, InputStream inputStream) | ||
{ | ||
new CipherInputStream(inputStream, computeCipher(password, Cipher.DECRYPT_MODE)) | ||
} | ||
|
||
static def withStreamToDecrypt(String password, InputStream inputStream, Closure closure) | ||
{ | ||
decryptStream(password, inputStream).withStream { closure(it) } | ||
} | ||
|
||
static OutputStream encryptStream(String password, OutputStream outputStream) | ||
{ | ||
new CipherOutputStream(outputStream, computeCipher(password, Cipher.ENCRYPT_MODE)) | ||
} | ||
|
||
static def withStreamToEncrypt(String password, OutputStream outputStream, Closure closure) | ||
{ | ||
encryptStream(password, outputStream).withStream { closure(it) } | ||
} | ||
|
||
private static Cipher computeCipher(String password, int mode) | ||
{ | ||
SecretKeySpec key = new SecretKeySpec(password.getBytes("UTF-8"), "Blowfish") | ||
Cipher cipher = Cipher.getInstance("Blowfish") | ||
cipher.init(mode, key) | ||
return cipher | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
utils/org.linkedin.glu.utils/src/test/groovy/test/utils/io/TestEncryptedStreams.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (c) 2012 Yan Pujante | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package test.utils.io | ||
|
||
import org.linkedin.glu.groovy.utils.io.GluGroovyIOUtils | ||
|
||
/** | ||
* @author yan@pongasoft.com */ | ||
public class TestEncryptedStreams extends GroovyTestCase | ||
{ | ||
public void testEncryptAndDecryptWithSamePassword() | ||
{ | ||
String text = "this is my test string" | ||
|
||
ByteArrayOutputStream baos = new ByteArrayOutputStream() | ||
|
||
String password = UUID.randomUUID().toString() | ||
|
||
GluGroovyIOUtils.withStreamToEncrypt(password, baos) { OutputStream os -> | ||
os << text | ||
} | ||
|
||
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()) | ||
|
||
baos = new ByteArrayOutputStream() | ||
|
||
GluGroovyIOUtils.withStreamToDecrypt(password, bais) { InputStream is -> | ||
baos << is | ||
} | ||
|
||
assertEquals(text, new String(baos.toByteArray(), "UTF-8")) | ||
|
||
baos = new ByteArrayOutputStream() | ||
|
||
GluGroovyIOUtils.withStreamToDecrypt("different", bais) { InputStream is -> | ||
baos << is | ||
} | ||
|
||
assertTrue(text != new String(baos.toByteArray(), "UTF-8")) | ||
|
||
} | ||
} |