Skip to content

Commit

Permalink
String#crypt needs a raw byte[] version from jnr-posix 3.0.11.
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Mar 24, 2015
1 parent 8aa9c0f commit e9ed3c0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion core/pom.xml
Expand Up @@ -105,7 +105,7 @@
<dependency>
<groupId>com.github.jnr</groupId>
<artifactId>jnr-posix</artifactId>
<version>3.0.10</version>
<version>3.0.11-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.github.jnr</groupId>
Expand Down
10 changes: 6 additions & 4 deletions core/src/main/java/org/jruby/RubyString.java
Expand Up @@ -2240,18 +2240,20 @@ public RubyString crypt(ThreadContext context, IRubyObject other) {
RubyString otherStr = other.convertToString().strDup(context.runtime);
otherStr.modify();
otherStr.associateEncoding(ascii8bit);
String salt = otherStr.asJavaString();
if (otherStr.getByteList().length() < 2) {
ByteList otherBL = otherStr.getByteList();
if (otherBL.length() < 2) {
throw context.runtime.newArgumentError("salt too short(need >=2 bytes)");
}

POSIX posix = context.runtime.getPosix();
CharSequence cryptedString = posix.crypt(asJavaString(), salt);
byte[] keyBytes = Arrays.copyOfRange(value.unsafeBytes(), value.begin(), value.realSize());
byte[] saltBytes = Arrays.copyOfRange(otherBL.unsafeBytes(), otherBL.begin(), otherBL.realSize());
byte[] cryptedString = posix.crypt(keyBytes, saltBytes);
// We differ from MRI in that we do not process salt to make it work and we will
// return any errors via errno.
if (cryptedString == null) throw context.runtime.newErrnoFromInt(posix.errno());

RubyString result = RubyString.newString(context.runtime, cryptedString.toString());
RubyString result = RubyString.newStringNoCopy(context.runtime, cryptedString, 0, cryptedString.length - 1);
result.associateEncoding(ascii8bit);
result.infectBy(this);
result.infectBy(otherStr);
Expand Down

0 comments on commit e9ed3c0

Please sign in to comment.