• Marley_06
  • NEWBIE
  • 10 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
I have this sample java code below that I need to translate into apex code for authentication. I already used the EncodingUtil and Crypto class to generate the password but I failed to connect to the service. The nonce parameter is a random generated string and the timeCreatedStr is a timestamp.

public static String getPasswordDigest(String nonce, String timeCreatedStr, String password) 
            throws UnsupportedEncodingException {
        
        ByteBuffer buf = ByteBuffer.allocate(1000);
        buf.put(Base64.decodeBase64(nonce));
        buf.put(timeCreatedStr.getBytes("UTF-8"));
        buf.put(password.getBytes("UTF-8"));
        byte[] toHash = new byte[buf.position()];
        buf.rewind();
        buf.get(toHash);
        
        System.out.println("@@@buf "+buf);
        
        byte[] hashSha256 = DigestUtils.sha256(toHash);
        String passwordDigestSha256 = Base64.encodeBase64String(hashSha256);
        return passwordDigestSha256;
    }
May I ask if there is a way to translate the above code into apex? May I know what is the use of the ByteBuffer? Is there any Bytebuffer class in apex?

Thank you!
I have this sample java code below that I need to translate into apex code for authentication. I already used the EncodingUtil and Crypto class to generate the password but I failed to connect to the service. The nonce parameter is a random generated string and the timeCreatedStr is a timestamp.

public static String getPasswordDigest(String nonce, String timeCreatedStr, String password) 
            throws UnsupportedEncodingException {
        
        ByteBuffer buf = ByteBuffer.allocate(1000);
        buf.put(Base64.decodeBase64(nonce));
        buf.put(timeCreatedStr.getBytes("UTF-8"));
        buf.put(password.getBytes("UTF-8"));
        byte[] toHash = new byte[buf.position()];
        buf.rewind();
        buf.get(toHash);
        
        System.out.println("@@@buf "+buf);
        
        byte[] hashSha256 = DigestUtils.sha256(toHash);
        String passwordDigestSha256 = Base64.encodeBase64String(hashSha256);
        return passwordDigestSha256;
    }
May I ask if there is a way to translate the above code into apex? May I know what is the use of the ByteBuffer? Is there any Bytebuffer class in apex?

Thank you!