2020年5月28日 星期四

【javascript】小數相加

javascript 小數相加偶爾會跑出奇怪的數字
例如:1.001 + 2.0001 = 3.00110000000000001

解決方式:先把小數乘很大的數,讓他變成整數計算好在除回去

剛剛的例子要改成:
(1.001 * 1e12) + (2.0001 * 1e12) / 1e12

(1e12 = 1後面帶了12個0,1000000000000)

2020年5月13日 星期三

【PHP】 AES 256 encrypt decrypt

AES(Advanced Encryption Standard) 256 進階加密標準
用來取代舊的DES(Data Encryption Standard)

php 的 mcrypt_encrypt 函數在php7.2就不支援要使用openssl_encrypt

使用openssl加解密方法:
function encrypt($key, $payload)
{
    $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
    $encrypted = openssl_encrypt($payload, 'aes-256-cbc', $key, 0, $iv);
    return base64_encode($encrypted . '::' . $iv);
}

function decrypt($key, $garble)
{
    list($encrypted_data, $iv) = explode('::', base64_decode($garble), 2);
    return openssl_decrypt($encrypted_data, 'aes-256-cbc', $key, 0, $iv);
}

$key = "ABCDE";
echo $encode = encrypt($key, "123"); 
echo "<br>";
echo $decode = decrypt($key, $encode); 

2020年1月17日 星期五

【linux】免費SSL設定,Cerbot

1.先到Cerbot官網

2.選擇目前伺服器環境

















3.照著步驟走,要先確定有裝mod_ssl
yum -y install yum-utils
yum-config-manager --enable rhui-REGION-rhel-server-extras rhui-REGION-rhel-server-optional

sudo yum install certbot python2-certbot-apache
sudo certbot --apache


4.取得憑證 certbot certonly --webroot -w /var/www/html -d mydomain.com --email {your_email_address} --agree-tos
成功後SSL憑證會放到 /etc/letsencrypt/live/{YOUR URL}/

5.改SSL,/etc/httpd/conf.d/ssl.conf
SSLCertificateFile /etc/letsencrypt/live/www.chiender.bnet.tw/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.chiender.bnet.tw/privkey.pem
SSLCACertificateFile /etc/letsencrypt/live/www.chiender.bnet.tw/fullchain.pem

6.設定VirturalHost
<VirtualHost *:80>
        ServerAlias *
        VirtualDocumentRoot "/var/www/html/%0"
        RewriteEngine on
        RewriteCond %{HTTPS} !=on
        RewriteRule ^(.*) https://%{SERVER_NAME}$1 [L,R]
</VirtualHost>

<VirtualHost *:443>
        ServerAlias *
        VirtualDocumentRoot "/var/www/html/%0"
        SSLEngine on
</VirtualHost>

2020年1月6日 星期一

【Jquery】ajax post from 檔案上傳

    $.ajax({
        type : "POST",
        url : "/sj/cooperation/ajax_dd",
        data : new FormData($('#myform')[0]),
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success : function($data) {
            $re = jQuery.parseJSON($data);
            $("#dd_id").val($re.dd_id);
            $msg = temp_dd($re.ddd);
            $($msg).hide().prependTo(".coop-msg").fadeIn('show');
            $("#coop_msg").val("");
        }
    });