Mercurial Hosting > luan
changeset 1649:f7e2df85fc0a
add String.digest_message
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 29 Mar 2022 16:07:01 -0600 |
parents | 224af797b1f9 |
children | cc3b10a94612 |
files | src/luan/modules/Binary.luan src/luan/modules/BinaryLuan.java src/luan/modules/String.luan src/luan/modules/StringLuan.java src/luan/modules/url/LuanUrl.java |
diffstat | 5 files changed, 28 insertions(+), 11 deletions(-) [+] |
line wrap: on
line diff
--- a/src/luan/modules/Binary.luan Mon Mar 28 18:00:12 2022 +0200 +++ b/src/luan/modules/Binary.luan Tue Mar 29 16:07:01 2022 -0600 @@ -10,10 +10,8 @@ Binary.base64_encode = Base64.getEncoder().encodeToString Binary.binary = BinaryLuan.binary Binary.byte = BinaryLuan.byte_ +Binary.digest_message = BinaryLuan.digest_message +Binary.to_hex = BinaryLuan.to_hex Binary.to_string = BinaryLuan.to_string -function Binary.digest_message(algorithm,input) - return MessageDigest.getInstance(algorithm).digest(input) -end - return Binary
--- a/src/luan/modules/BinaryLuan.java Mon Mar 28 18:00:12 2022 +0200 +++ b/src/luan/modules/BinaryLuan.java Tue Mar 29 16:07:01 2022 -0600 @@ -1,6 +1,8 @@ package luan.modules; import java.io.UnsupportedEncodingException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import luan.Luan; import luan.LuanTable; import luan.LuanFunction; @@ -55,4 +57,16 @@ return charsetName!=null ? new String(binary,charsetName) : toString(binary); } + public static byte[] digest_message(String algorithm,byte[] input) throws NoSuchAlgorithmException { + return MessageDigest.getInstance(algorithm).digest(input); + } + + public static String to_hex(byte[] a) { + StringBuilder sb = new StringBuilder(); + for( byte b : a ) { + sb.append( String.format("%02x",b) ); + } + return sb.toString(); + } + }
--- a/src/luan/modules/String.luan Mon Mar 28 18:00:12 2022 +0200 +++ b/src/luan/modules/String.luan Tue Mar 29 16:07:01 2022 -0600 @@ -5,6 +5,7 @@ local String = {} String.char = StringLuan.char_ +String.digest_message = StringLuan.digest_message String.encode = StringLuan.encode String.find = StringLuan.find String.format = StringLuan.format
--- a/src/luan/modules/StringLuan.java Mon Mar 28 18:00:12 2022 +0200 +++ b/src/luan/modules/StringLuan.java Tue Mar 29 16:07:01 2022 -0600 @@ -1,5 +1,6 @@ package luan.modules; +import java.security.NoSuchAlgorithmException; import java.util.Arrays; import java.util.regex.Pattern; import java.util.regex.Matcher; @@ -236,4 +237,10 @@ return s.split(pattern,n); } + public static String digest_message(String algorithm,String input) throws LuanException, NoSuchAlgorithmException { + Utils.checkNotNull(algorithm,1); + Utils.checkNotNull(input,2); + return BinaryLuan.to_hex( BinaryLuan.digest_message( algorithm, input.getBytes() ) ); + } + }
--- a/src/luan/modules/url/LuanUrl.java Mon Mar 28 18:00:12 2022 +0200 +++ b/src/luan/modules/url/LuanUrl.java Tue Mar 29 16:07:01 2022 -0600 @@ -25,6 +25,7 @@ import luan.LuanJavaFunction; import luan.LuanException; import luan.modules.IoLuan; +import luan.modules.StringLuan; import luan.modules.Utils; @@ -379,15 +380,11 @@ return path; } - // retarded java api lacks this public static String md5(String s) { try { - byte[] md5 = MessageDigest.getInstance("MD5").digest(s.getBytes()); - StringBuffer sb = new StringBuffer(); - for( byte b : md5 ) { - sb.append( String.format("%02x",b) ); - } - return sb.toString(); + return StringLuan.digest_message("MD5",s); + } catch(LuanException e) { + throw new RuntimeException(e); } catch(NoSuchAlgorithmException e) { throw new RuntimeException(e); }