B4J Question string to sha256 + salt

Discussion in 'B4J Questions' started by Douglas Farias, Sep 9, 2015.

  1. Douglas Farias

    Douglas Farias Expert Licensed User

    Hi All
    i dont found this exacty question on the forum, how can i make a string sha256 + salt?
    i need send this to php file hosted on a server, but dont find on the forum about AES-256

    thats the php code
    PHP:
    class Security {
        
        
    # Private key
        
    public static $salt 'ZfTfbip&Gs0Z4yz34jFrG)Ha0gahptzLN7ROi%gy';
        
    # Encrypt a value using AES-256.
        
    public static function encrypt($plain$key$hmacSalt null) {
            
    self::_checkKey($key'encrypt()');
            if (
    $hmacSalt === null) {
                
    $hmacSalt self::$salt;
            }
            
    $key substr(hash('sha256'$key $hmacSalt), 032); # Generate the encryption and hmac key
            
    $algorithm MCRYPT_RIJNDAEL_128# encryption algorithm
            
    $mode MCRYPT_MODE_CBC# encryption mode
            
    $ivSize mcrypt_get_iv_size($algorithm$mode); # Returns the size of the IV belonging to a specific cipher/mode combination
            
    $iv mcrypt_create_iv($ivSizeMCRYPT_DEV_URANDOM); # Creates an initialization vector (IV) from a random source
            
    $ciphertext $iv mcrypt_encrypt($algorithm$key$plain$mode$iv); # Encrypts plaintext with given parameters
            
    $hmac hash_hmac('sha256'$ciphertext$key); # Generate a keyed hash value using the HMAC method
            
    return $hmac $ciphertext;
        }
        
    # Check key
        
    protected static function _checkKey($key$method) {
            if (
    strlen($key) < 32) {
                echo 
    "Invalid public key $key, key must be at least 256 bits (32 bytes) long."; die();
            }
        }
        
    # Decrypt a value using AES-256.
        
    public static function decrypt($cipher$key$hmacSalt null) {
            
    self::_checkKey($key'decrypt()');
            if (empty(
    $cipher)) {
                echo 
    'The data to decrypt cannot be empty.'; die();
            }
            if (
    $hmacSalt === null) {
                
    $hmacSalt self::$salt;
            }
            
    $key substr(hash('sha256'$key $hmacSalt), 032); # Generate the encryption and hmac key.
            # Split out hmac for comparison
            
    $macSize 64;
            
    $hmac substr($cipher0$macSize);
            
    $cipher substr($cipher$macSize);
            
    $compareHmac hash_hmac('sha256'$cipher$key);
            if (
    $hmac !== $compareHmac) {
                return 
    false;
            }
            
    $algorithm MCRYPT_RIJNDAEL_128# encryption algorithm
            
    $mode MCRYPT_MODE_CBC# encryption mode
            
    $ivSize mcrypt_get_iv_size($algorithm$mode); # Returns the size of the IV belonging to a specific cipher/mode combination
            
    $iv substr($cipher0$ivSize);
            
    $cipher substr($cipher$ivSize);
            
    $plain mcrypt_decrypt($algorithm$key$cipher$mode$iv);
            return 
    rtrim($plain"\0");
        }
        
        
    # Get Random String - Usefull for public key
        
    public function genRandString($length 0) {
            
    $charset 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
            
    $str '';
            
    $count strlen($charset);
            while (
    $length-- > 0) {
                
    $str .= $charset[mt_rand(0$count-1)];
            }
        return 
    $str;
        }
    }
    how make this in b4j? and what libs i need use?
     
  2. Douglas Farias

    Douglas Farias Expert Licensed User

    solved, the same libs of b4a works here on b4j *-* ty
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice