Search code examples
javascriptjavahashsha512

how to convert this code in javascript to java


i have this code in javascript and i need to convert it to java but im not expert in javascript.

here the code:

function keyGen(mat)
{
        var hash = base64_encode(pack("H*", sha1($mat)));
        var l = obj.hash.length - 4;
        var p1 = Math.floor(Math.random() * (l+1)) ;
        var p2 = Math.floor(Math.random() * (l+1)) ;
        var p3 = Math.floor(Math.random() * (l+1)) ;
        var motif1 = obj.hash.substr(p1, 4) ;
        var motif2 = obj.hash.substr(p2, 4) ;
        var motif3 = obj.hash.substr(p3, 4) ;
        var cle = motif1+motif2+motif3 ;
    return cle ;
}

for the hash i use the function but i can't to modify it:

public static String get_SHA_512_SecurePassword(String passwordToHash, String   salt){
        String generatedPassword = null;
            try {
                 MessageDigest md = MessageDigest.getInstance("SHA-512");
                 md.update(salt.getBytes(StandardCharsets.UTF_8));
                 byte[] bytes = md.digest(passwordToHash.getBytes(StandardCharsets.UTF_8));
                 StringBuilder sb = new StringBuilder();
                 for(int i=0; i< bytes.length ;i++){
                    sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
                 }
                 generatedPassword = sb.toString();
                } 
               catch (NoSuchAlgorithmException e){
                e.printStackTrace();
               }
            return generatedPassword;
        }

Solution

  • here is the full code : i found it ..... thanks all

    public static String get_SHA_512_SecurePassword(String passwordToHash, String   salt){
            String generatedPassword = null;
                try {
                     MessageDigest md = MessageDigest.getInstance("SHA-512");
                     md.update(salt.getBytes(StandardCharsets.UTF_8));
                     byte[] bytes = md.digest(passwordToHash.getBytes(StandardCharsets.UTF_8));
                     StringBuilder sb = new StringBuilder();
                     for(int i=0; i< bytes.length ;i++){
                        sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
                     }
                //     String base64 = Base64.encodeBase64(sb.toString());
                     generatedPassword = sb.toString();
                    } 
                   catch (NoSuchAlgorithmException e){
                    e.printStackTrace();
                   }
                return generatedPassword;
            }
    
        public static String keyGen(String Studentpass,String SaltCrypter) {
            String hash=get_SHA_512_SecurePassword(Studentpass,SaltCrypter);
    
            int  l = hash.length() - 4;
            new Random().nextInt();
    
            int p1 =  new  Random().nextInt(l) ; 
            int p2 =  new  Random().nextInt(l) ; 
            int p3 =  new  Random().nextInt(l) ; 
    
            String motif1 = hash.substring(p1, p1+4);
            String motif2 = hash.substring(p2, p2+4);
            String motif3 = hash.substring(p3, p3+4);
            String cle =(motif1+motif2+motif3);
            cle.replace("l", "L").replace("O", "o").replace("I", "i");
            return cle;
        }