Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #4 from thecabinet/master
nqp::sha1
  • Loading branch information
jnthn committed Jan 10, 2013
2 parents 11a30be + 93418f5 commit baf26c3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/QAST/JASTCompiler.nqp
Expand Up @@ -980,6 +980,7 @@ QAST::OperationsJAST.map_classlib_core_op('lc', $TYPE_OPS, 'lc', [$RT_STR], $RT_
QAST::OperationsJAST.map_classlib_core_op('x', $TYPE_OPS, 'x', [$RT_STR, $RT_INT], $RT_STR);
QAST::OperationsJAST.map_classlib_core_op('concat', $TYPE_OPS, 'concat', [$RT_STR, $RT_STR], $RT_STR);
QAST::OperationsJAST.map_classlib_core_op('chr', $TYPE_OPS, 'chr', [$RT_INT], $RT_STR);
QAST::OperationsJAST.map_classlib_core_op('sha1', $TYPE_OPS, 'sha1', [$RT_STR], $RT_STR);

#bitwise opcodes
QAST::OperationsJAST.map_classlib_core_op('bitor_i', $TYPE_OPS, 'bitor_i', [$RT_INT, $RT_INT], $RT_INT);
Expand Down
16 changes: 16 additions & 0 deletions src/org/perl6/nqp/runtime/Ops.java
@@ -1,6 +1,9 @@
package org.perl6.nqp.runtime;

import java.math.BigInteger;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;

import org.perl6.nqp.sixmodel.*;
Expand Down Expand Up @@ -814,6 +817,19 @@ public static String chr(long val) {
return (new StringBuffer()).append((char) val).toString();
}

public static String sha1(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest md = MessageDigest.getInstance("SHA1");

byte[] inBytes = str.getBytes("UTF-8");
byte[] outBytes = md.digest(inBytes);

StringBuilder sb = new StringBuilder();
for (byte b : outBytes) {
sb.append(String.format("%02X", b));
}
return sb.toString();
}

/* bitwise operations. */
public static long bitor_i(long valA, long valB) {
return valA | valB;
Expand Down
23 changes: 21 additions & 2 deletions t/qast_string.t
@@ -1,6 +1,6 @@
use helper;

plan(6);
plan(7);

qast_test(
-> {
Expand Down Expand Up @@ -116,4 +116,23 @@ qast_test(
)))
},
"B\n",
"chr");
"chr");

qast_test(
-> {
my $block := QAST::Block.new(
QAST::Op.new(
:op('say'),
QAST::Op.new(
:op('sha1'),
QAST::SVal.new( :value("larva") )
)));
QAST::CompUnit.new(
$block,
:main(QAST::Op.new(
:op('call'),
QAST::BVal.new( :value($block) )
)))
},
"2DE6BA12D336DD56ABE5B163DDF836B951A2CA7C\n",
"sha1");

0 comments on commit baf26c3

Please sign in to comment.