Skip to content

Commit

Permalink
fix bug with file decryption number of bytes calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
rockaport committed Feb 12, 2017
1 parent 3e97c7f commit 5caf5cf
Show file tree
Hide file tree
Showing 24 changed files with 5,163 additions and 4,154 deletions.
7 changes: 4 additions & 3 deletions alice/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'jacoco'

version = '0.3'
version = '0.5'

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
Expand All @@ -12,10 +12,11 @@ dependencies {

compile "org.codehaus.groovy:groovy-all:2.4.1"

testCompile 'org.spockframework:spock-core:1.1-groovy-2.4-rc-3'
testCompile group: 'commons-io', name: 'commons-io', version: '2.5'
testCompile group: 'org.apache.commons', name: 'commons-lang3', version: '3.5'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.0.0-M3'
testRuntime group: 'org.junit.vintage', name: 'junit-vintage-engine', version: '4.12.0-M3'
testCompile 'org.spockframework:spock-core:1.1-groovy-2.4-rc-1'
testCompile group: 'commons-io', name: 'commons-io', version: '2.5'
}

task copyDocs(dependsOn: 'javadoc', type: Copy) {
Expand Down
8 changes: 8 additions & 0 deletions alice/src/main/java/com/rockaport/alice/Alice.java
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,10 @@ public synchronized void decrypt(File input, File output, char[] password)

RandomAccessFile randomAccessFile = new RandomAccessFile(input, "r");

if (randomAccessFile.length() - mac.getMacLength() <= 0) {
throw new IOException("File oes not contain sufficient data for decryption");
}

randomAccessFile.seek(randomAccessFile.length() - mac.getMacLength());
randomAccessFile.read(recMac);

Expand Down Expand Up @@ -374,6 +378,10 @@ public synchronized void decrypt(File input, File output, char[] password)
while ((bytesRead = bufferedInputStream.read(inputStreamBuffer)) > 0) {
numBytesToProcess = (bytesRead < bytesLeft) ? bytesRead : (int) bytesLeft;

if (numBytesToProcess <= 0) {
break;
}

bufferedOutputStream.write(cipher.update(inputStreamBuffer, 0, numBytesToProcess));

// reduce the number of bytes left
Expand Down
6 changes: 1 addition & 5 deletions alice/src/main/java/com/rockaport/alice/AliceContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public enum Pbkdf {
/**
* Use password as is.
*/
NONE,
NONE("None"),
/**
* SHA-1 hash the password
*/
Expand Down Expand Up @@ -233,10 +233,6 @@ public enum Pbkdf {

private final String value;

Pbkdf() {
this("");
}

Pbkdf(String value) {
this.value = value;
}
Expand Down
54 changes: 52 additions & 2 deletions alice/src/test/groovy/com.rockaport.alice/AliceTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.rockaport.alice

import com.rockaport.alice.AliceContext
import org.apache.commons.io.FileUtils
import org.apache.commons.lang3.RandomStringUtils
import spock.lang.Shared
import spock.lang.Specification

Expand All @@ -11,7 +12,7 @@ class AliceTest extends Specification {
@Shared
badPassword = "badPassword".chars
@Shared
plainText = "Some message to ".bytes
plainText = RandomStringUtils.randomAscii(16 * 1024).bytes
@Shared
inputFileName = "input.dat"
@Shared
Expand Down Expand Up @@ -113,10 +114,31 @@ class AliceTest extends Specification {
null | null | null || IllegalArgumentException
null | null | new char[0] || IllegalArgumentException
null | null | new char[1] || IllegalArgumentException

null | new File(encryptedFileName) | null || IllegalArgumentException
null | new File(encryptedFileName) | new char[0] || IllegalArgumentException
null | new File(encryptedFileName) | new char[1] || IllegalArgumentException

new File(nonExistentFileName) | null | null || IllegalArgumentException
new File(nonExistentFileName) | null | new char[0] || IllegalArgumentException
new File(nonExistentFileName) | null | new char[1] || IllegalArgumentException

new File(nonExistentFileName) | new File(encryptedFileName) | null || IllegalArgumentException
new File(nonExistentFileName) | new File(encryptedFileName) | new char[0] || IllegalArgumentException
new File(nonExistentFileName) | new File(encryptedFileName) | new char[1] || IllegalArgumentException

new File(emptyFileName) | null | null || IllegalArgumentException
new File(emptyFileName) | null | new char[0] || IllegalArgumentException
new File(emptyFileName) | null | new char[1] || IllegalArgumentException

new File(emptyFileName) | new File(encryptedFileName) | null || IllegalArgumentException
new File(emptyFileName) | new File(encryptedFileName) | new char[0] || IllegalArgumentException
new File(emptyFileName) | new File(encryptedFileName) | new char[1] || IllegalArgumentException

new File(inputFileName) | null | null || IllegalArgumentException
new File(inputFileName) | null | new char[0] || IllegalArgumentException
new File(inputFileName) | null | new char[1] || IllegalArgumentException

new File(inputFileName) | new File(encryptedFileName) | null || IllegalArgumentException
new File(inputFileName) | new File(encryptedFileName) | new char[0] || IllegalArgumentException
}
Expand Down Expand Up @@ -154,13 +176,41 @@ class AliceTest extends Specification {
null | null | null || IllegalArgumentException
null | null | new char[0] || IllegalArgumentException
null | null | new char[1] || IllegalArgumentException

null | new File(decryptedFileName) | null || IllegalArgumentException
null | new File(decryptedFileName) | new char[0] || IllegalArgumentException
null | new File(decryptedFileName) | new char[1] || IllegalArgumentException

new File(nonExistentFileName) | null | null || IllegalArgumentException
new File(nonExistentFileName) | null | new char[0] || IllegalArgumentException
new File(nonExistentFileName) | null | new char[1] || IllegalArgumentException

new File(nonExistentFileName) | new File(decryptedFileName) | null || IllegalArgumentException
new File(nonExistentFileName) | new File(decryptedFileName) | new char[0] || IllegalArgumentException
new File(nonExistentFileName) | new File(decryptedFileName) | new char[1] || IllegalArgumentException

new File(emptyFileName) | null | null || IllegalArgumentException
new File(emptyFileName) | null | new char[0] || IllegalArgumentException
new File(emptyFileName) | null | new char[1] || IllegalArgumentException

new File(emptyFileName) | new File(decryptedFileName) | null || IllegalArgumentException
new File(emptyFileName) | new File(decryptedFileName) | new char[0] || IllegalArgumentException
new File(emptyFileName) | new File(decryptedFileName) | new char[1] || IllegalArgumentException

new File(invalidFileName) | null | null || IllegalArgumentException
new File(invalidFileName) | null | new char[0] || IllegalArgumentException
new File(invalidFileName) | null | new char[1] || IllegalArgumentException

new File(invalidFileName) | new File(decryptedFileName) | null || IllegalArgumentException
new File(invalidFileName) | new File(decryptedFileName) | new char[0] || IllegalArgumentException
new File(invalidFileName) | new File(decryptedFileName) | new char[1] || IOException

new File(encryptedFileName) | null | null || IllegalArgumentException
new File(encryptedFileName) | null | new char[0] || IllegalArgumentException
new File(encryptedFileName) | null | new char[1] || IllegalArgumentException

new File(encryptedFileName) | new File(decryptedFileName) | null || IllegalArgumentException
new File(encryptedFileName) | new File(decryptedFileName) | new char[0] || IllegalArgumentException
new File(invalidFileName) | new File(decryptedFileName) | new char[1] || IOException
}

def "Invalid iterations throws exception"() {
Expand Down
41 changes: 25 additions & 16 deletions docs/allclasses-frame.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,35 @@
<!-- NewPage -->
<html lang="en">
<head>
<!-- Generated by javadoc (1.8.0_111) on Thu Feb 09 21:55:04 EST 2017 -->
<title>All Classes (alice 0.3 API)</title>
<meta name="date" content="2017-02-09">
<link rel="stylesheet" type="text/css" href="stylesheet.css" title="Style">
<script type="text/javascript" src="script.js"></script>
<!-- Generated by javadoc (1.8.0_111) on Sun Feb 12 16:49:21 EST 2017 -->
<title>All Classes (alice 0.5 API)</title>
<meta name="date" content="2017-02-12">
<link rel="stylesheet" type="text/css" href="stylesheet.css" title="Style">
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<h1 class="bar">All&nbsp;Classes</h1>
<div class="indexContainer">
<ul>
<li><a href="com/rockaport/alice/Alice.html" title="class in com.rockaport.alice" target="classFrame">Alice</a></li>
<li><a href="com/rockaport/alice/AliceContext.html" title="class in com.rockaport.alice" target="classFrame">AliceContext</a></li>
<li><a href="com/rockaport/alice/AliceContext.Algorithm.html" title="enum in com.rockaport.alice" target="classFrame">AliceContext.Algorithm</a></li>
<li><a href="com/rockaport/alice/AliceContext.KeyLength.html" title="enum in com.rockaport.alice" target="classFrame">AliceContext.KeyLength</a></li>
<li><a href="com/rockaport/alice/AliceContext.MacAlgorithm.html" title="enum in com.rockaport.alice" target="classFrame">AliceContext.MacAlgorithm</a></li>
<li><a href="com/rockaport/alice/AliceContext.Mode.html" title="enum in com.rockaport.alice" target="classFrame">AliceContext.Mode</a></li>
<li><a href="com/rockaport/alice/AliceContext.Padding.html" title="enum in com.rockaport.alice" target="classFrame">AliceContext.Padding</a></li>
<li><a href="com/rockaport/alice/AliceContext.Pbkdf.html" title="enum in com.rockaport.alice" target="classFrame">AliceContext.Pbkdf</a></li>
<li><a href="com/rockaport/alice/AliceContextBuilder.html" title="class in com.rockaport.alice" target="classFrame">AliceContextBuilder</a></li>
</ul>
<ul>
<li><a href="com/rockaport/alice/Alice.html" title="class in com.rockaport.alice" target="classFrame">Alice</a>
</li>
<li><a href="com/rockaport/alice/AliceContext.html" title="class in com.rockaport.alice" target="classFrame">AliceContext</a>
</li>
<li><a href="com/rockaport/alice/AliceContext.Algorithm.html" title="enum in com.rockaport.alice"
target="classFrame">AliceContext.Algorithm</a></li>
<li><a href="com/rockaport/alice/AliceContext.KeyLength.html" title="enum in com.rockaport.alice"
target="classFrame">AliceContext.KeyLength</a></li>
<li><a href="com/rockaport/alice/AliceContext.MacAlgorithm.html" title="enum in com.rockaport.alice"
target="classFrame">AliceContext.MacAlgorithm</a></li>
<li><a href="com/rockaport/alice/AliceContext.Mode.html" title="enum in com.rockaport.alice"
target="classFrame">AliceContext.Mode</a></li>
<li><a href="com/rockaport/alice/AliceContext.Padding.html" title="enum in com.rockaport.alice"
target="classFrame">AliceContext.Padding</a></li>
<li><a href="com/rockaport/alice/AliceContext.Pbkdf.html" title="enum in com.rockaport.alice"
target="classFrame">AliceContext.Pbkdf</a></li>
<li><a href="com/rockaport/alice/AliceContextBuilder.html" title="class in com.rockaport.alice"
target="classFrame">AliceContextBuilder</a></li>
</ul>
</div>
</body>
</html>
39 changes: 23 additions & 16 deletions docs/allclasses-noframe.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,33 @@
<!-- NewPage -->
<html lang="en">
<head>
<!-- Generated by javadoc (1.8.0_111) on Thu Feb 09 21:55:04 EST 2017 -->
<title>All Classes (alice 0.3 API)</title>
<meta name="date" content="2017-02-09">
<link rel="stylesheet" type="text/css" href="stylesheet.css" title="Style">
<script type="text/javascript" src="script.js"></script>
<!-- Generated by javadoc (1.8.0_111) on Sun Feb 12 16:49:21 EST 2017 -->
<title>All Classes (alice 0.5 API)</title>
<meta name="date" content="2017-02-12">
<link rel="stylesheet" type="text/css" href="stylesheet.css" title="Style">
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<h1 class="bar">All&nbsp;Classes</h1>
<div class="indexContainer">
<ul>
<li><a href="com/rockaport/alice/Alice.html" title="class in com.rockaport.alice">Alice</a></li>
<li><a href="com/rockaport/alice/AliceContext.html" title="class in com.rockaport.alice">AliceContext</a></li>
<li><a href="com/rockaport/alice/AliceContext.Algorithm.html" title="enum in com.rockaport.alice">AliceContext.Algorithm</a></li>
<li><a href="com/rockaport/alice/AliceContext.KeyLength.html" title="enum in com.rockaport.alice">AliceContext.KeyLength</a></li>
<li><a href="com/rockaport/alice/AliceContext.MacAlgorithm.html" title="enum in com.rockaport.alice">AliceContext.MacAlgorithm</a></li>
<li><a href="com/rockaport/alice/AliceContext.Mode.html" title="enum in com.rockaport.alice">AliceContext.Mode</a></li>
<li><a href="com/rockaport/alice/AliceContext.Padding.html" title="enum in com.rockaport.alice">AliceContext.Padding</a></li>
<li><a href="com/rockaport/alice/AliceContext.Pbkdf.html" title="enum in com.rockaport.alice">AliceContext.Pbkdf</a></li>
<li><a href="com/rockaport/alice/AliceContextBuilder.html" title="class in com.rockaport.alice">AliceContextBuilder</a></li>
</ul>
<ul>
<li><a href="com/rockaport/alice/Alice.html" title="class in com.rockaport.alice">Alice</a></li>
<li><a href="com/rockaport/alice/AliceContext.html" title="class in com.rockaport.alice">AliceContext</a></li>
<li><a href="com/rockaport/alice/AliceContext.Algorithm.html" title="enum in com.rockaport.alice">AliceContext.Algorithm</a>
</li>
<li><a href="com/rockaport/alice/AliceContext.KeyLength.html" title="enum in com.rockaport.alice">AliceContext.KeyLength</a>
</li>
<li><a href="com/rockaport/alice/AliceContext.MacAlgorithm.html" title="enum in com.rockaport.alice">AliceContext.MacAlgorithm</a>
</li>
<li><a href="com/rockaport/alice/AliceContext.Mode.html"
title="enum in com.rockaport.alice">AliceContext.Mode</a></li>
<li><a href="com/rockaport/alice/AliceContext.Padding.html" title="enum in com.rockaport.alice">AliceContext.Padding</a>
</li>
<li><a href="com/rockaport/alice/AliceContext.Pbkdf.html"
title="enum in com.rockaport.alice">AliceContext.Pbkdf</a></li>
<li><a href="com/rockaport/alice/AliceContextBuilder.html" title="class in com.rockaport.alice">AliceContextBuilder</a>
</li>
</ul>
</div>
</body>
</html>
Loading

0 comments on commit 5caf5cf

Please sign in to comment.