Tuesday, 10 July 2018

password_verify() returns false for correct password

Hey guys, I've just recently learned about the new hashing functions of PHP5.5+, but unfortunately I'm getting mixed results after I deciding to try them out myself.
Here is the code, nothing else is on the page:
<?php
$password = "Hello";
$hash = password_hash($password, PASSWORD_DEFAULT);

echo $hash;
?>
Whenever I echo this value, and copy it from the page to the function:
<?php 

var_dump(password_verify("Hello", "Hash I copied from rendered page));

?>
It returns bool(false).
On the other hand, if I do:
<?php

$password = "Hello";
$hash = password_hash($password, PASSWORD_DEFAULT);
echo $hash;

var_dump(password_verify("Hello", $hash));

?>
It returns true.
Is there some sort of formatting or security measure being applied to the echoed $hash?? I've tried google to no success.
Using password_get_info() on the copied $hash I get the following:
array(3) { ["algo"]=> int(0) ["algoName"]=> string(7) "unknown" ["options"]=> array(0) { } }
Something is clearly being lost here.
Thank you for your time guys.

Wednesday, 30 May 2018

How do I get a YouTube video thumbnail from the YouTube API?

Each YouTube video has 4 generated images. They are predictably formatted as follows:
https://img.youtube.com/vi/<insert-youtube-video-id-here>/0.jpg
https://img.youtube.com/vi/<insert-youtube-video-id-here>/1.jpg
https://img.youtube.com/vi/<insert-youtube-video-id-here>/2.jpg
https://img.youtube.com/vi/<insert-youtube-video-id-here>/3.jpg
The first one in the list is a full size image and others are thumbnail images. The default thumbnail image (ie. one of 1.jpg, 2.jpg, 3.jpg) is:
https://img.youtube.com/vi/<insert-youtube-video-id-here>/default.jpg
For the high quality version of the thumbnail use a url similar to this:
https://img.youtube.com/vi/<insert-youtube-video-id-here>/hqdefault.jpg
There is also a medium quality version of the thumbnail, using a url similar to the HQ:
https://img.youtube.com/vi/<insert-youtube-video-id-here>/mqdefault.jpg
For the standard definition version of the thumbnail, use a url similar to this:
https://img.youtube.com/vi/<insert-youtube-video-id-here>/sddefault.jpg
For the maximum resolution version of the thumbnail use a url similar to this:
https://img.youtube.com/vi/<insert-youtube-video-id-here>/maxresdefault.jpg
All of the above urls are available over http too. Additionally, the slightly shorter hostname i3.ytimg.com works in place of img.youtube.com in the example urls above.
Alternatively, you can use the YouTube Data API (v3) to get thumbnail images.

Tuesday, 29 May 2018

Python: Validate Phone Numbers

Phone numbers can be validated very easily using python-phonenumbers.
Here is a short example demonstrating the use of phonenumbers package.  I am using "None" as the country. Any country can be used such as e.g. "GB", "US"
Remember to

pip install phonenumbers
And

import phonenumbers;

def validate_phone(value):
    try:
        parsed_number = phonenumbers.parse(value, None)
        if not phonenumbers.is_valid_number(parsed_number) or 
           not phonenumbers.is_possible_number(parsed_number):
               print("Please provide a valid phone number e.g +CountryCodeNumber")
               return False
    except phonenumbers.phonenumberutil.NumberParseException:
               print("Please provide a valid phone number e.g +CountryCodeNumber")
               return False
 return True

Monday, 28 May 2018

Could not open input file: artisan

You need to first create Laravel project  and if you already have one you need to go to this project dir using cd command in terminal for example cd myproject.
Now you will be able to run any artisan commands, for example running php artisan serve will run default project.

PHP PDO Insert Method

<?php
class Contact
{
    private $UploadedFiles = '';
    private $DbHost = DB_HOST;
    private $DbName = DB_NAME;
    private $DbUser = DB_USER;
    private $DbPass = DB_PASS;
    private $table;

    function __construct()
    {
        $this->table = strtolower(get_class());
    }

    public function insert($values = array())
    {
        $dbh = new PDO("mysql:host=$this->DbHost;dbname=$this->DbName", $this->DbUser, $this->DbPass, array(PDO::ATTR_PERSISTENT => true));
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $dbh->exec("SET CHARACTER SET utf8");

        foreach ($values as $field => $v)
            $ins[] = ':' . $field;

        $ins = implode(',', $ins);
        $fields = implode(',', array_keys($values));
        $sql = "INSERT INTO $this->table ($fields) VALUES ($ins)";

        $sth = $dbh->prepare($sql);
        foreach ($values as $f => $v)
        {
            $sth->bindValue(':' . $f, $v);
        }
        $sth->execute();
        //return $this->lastId = $dbh->lastInsertId();
    }

}

Thursday, 24 May 2018

How to use virtualenv with python3.6 on ubuntu16.04

I encountered this problem while using Ubuntu 16.04. When I created virtual environment it used python2.x which is already there in Ubuntu.

python3 -m venv .

Check if your virtual environment is using latest python 3 by activating virtual environemnt by:

source bin/activate

python -V

Below is the older method of doing the same. Try if the above method is not working:

 
Here is how to switch to python 3.6

sudo apt-get install python-virtualenv
virtualenv --python=python3.6 venv

venv is the name of virtual environment

if there is an error that python3-env is not located then run

sudo apt install python3.6-venv

-----------------------------------------------------------------------------------------------------------------------
OR

sudo apt install -y python3-venv
python3 -m venv env 
(env is the name of the python environment)

Finally, activate your virtual environment.

source venv/bin/activate

We can use
 which python
To check which python is being used once you have activated your virtual environment.


Wednesday, 23 May 2018

Django: Delete Postgres database in Ubuntu

Run the following commands to go into Postgres command line:

sudo su postgres
psql

Now the database can be dropped by using the following command:

drop database database_name;

Tuesday, 10 February 2015

Android: TableLayout Round Corners

Create an XML file (name: roundtablecorner.xml) in the drawable folder. Copy the following contents in the file:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#006600" />

    <corners android:radius="10dp" />

    <padding
        android:bottom="0dp"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp" />

</shape>

Then use the above (roundtablecorner.xml) file as the background for a TableLayout

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/roundtablecorner">

Choose the background color in the shape in:

    <solid android:color="#006600" />