[컴][nodejs] AdonisJS 설치

adonis debugger setting / 디버거 세팅


AdonisJS

일단 이녀석은 php 의 framework Laravel 의 구조를 그대로 가져왔다 .그래서 Laravel 을 사용해 봤다면 이해가 어렵지 않아 보인다.

AdonisJs CLI 설치

이녀석을 설치하면, 추후에 작업을 하는것이 좀 더 수월하다. 반복적인 작업들(파일생성등) 을 할 때 용이한 듯 하다.

npm i @adonisjs/cli

project 생성

일단 project 를 생성해 보자. 아래처럼 하면 myproj 라는 project 가 하나 생성된다.
adonis.cmd new myproj

.env

일단 설치를 하고 나면, .env 설정을 해줘야 하는데, key:generate 를 해주면 자동으로 생성된다.

cd myproj
adonis.cmd key:generate

run server

adonis.cmd serve --dev


unit test 

아래처럼 실행하면 된다.
c:\project_root> adonis test 
c:\project_root> adonis test --files invest-limit.spec.js,auth.spec.js 



VSCode, typescript


vscode debugger 세팅하기

launch.js

먼저 vscode 내의 terminal 에서 adonis serve --dev 로 서버를 실행한다. 그리고 나서 vscode를 열고, debugger 를 실행하면 된다.(attach)

또는 아래 'launch' 를 이용해서 바로 실행해도 된다.

launch.js 는 아래처럼 설정하면 된다.
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        
        {
            "type": "node",
            "request": "attach",
            "name": "Attach by Process ID",
            "processId": "${command:PickProcess}"
        },
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "program": "${workspaceFolder}/app.js",
            "args": [
                "--dev",
            ]
        },
        {
            "type": "node",
            "request": "launch",
            "name": "Unit test Launch Program",
            "program": "${workspaceFolder}/test_ace.js",
            "args": [
                "test",
                "-f",
                "${fileBasename}"
            ],
            "internalConsoleOptions": "openOnSessionStart",
        },
    ]
}

실행

실행을 하면 attach 할 process 를 선택하라고 나온다. 이때 adonis server --dev 외에 다른 프로세스를 선택한다.

unit test 실행

unit test 를 하면서 관련해서 debugger mode 를 사용하려면, 위처럼 하면 된다.

test_ace.js
ace file 을 그대로 실행할 수 없다. .js file 이 필요해서 아래처럼 .js file 을 만들면 된다.

'use strict'

require('./ace');

cmd 에서 바로 unittest 실행

node test_ace.js test -f myclass.spec.js



다른 방법

서버 실행

node --inspect server.js

debugger 붙이기(attach)

기본적으로 node 가 9229 로 debugger port를 연다.
{
    "type": "node",
    "request": "attach",
    "name": "Attach to localhost",
    "address": "localhost",
    "port": 9229,
    "localRoot": "${workspaceFolder}",
    "remoteRoot": "${workspaceFolder}"
},

See Also

  1. AdoniJS at Start up, 2019-02-07
  2. [컴][웹] Adonijs Unittest 사용시 Tips

References

  1. Installation Adonis JS 4.1



[컴] Windows 용 Docker 설치

docker for windows


Windows 에서 docker 설치

Installation

위 경로에서 download 를 하면 된다.

prerequisite 전제조건

  • Microsoft Hyper-V 가 실행돼야 한다.: The Docker for Windows installer 가 enable 해준다.
  • 옛날 windows 의 docker toolbox는 VM (Virtual Box)을 사용했었는데, 이제는 VM 을 사용하지 않는다. : Docker for Mac vs. Docker Toolbox  를 읽어보자.

System Requirements

아래 조건을 만족해야 한다. 만족하지 않아도 docker toolbox 를 사용할 수는 있다.

  • Windows 10 64bit: Pro, Enterprise or Education (1607 Anniversary Update, Build 14393 or later).
  • BIOS 에서 Virtualization 을 켜야 한다.(기본적으로 켜져있다.)
  • CPU SLAT-capable feature.
  • At least 4GB of RAM.

설치마법사







설치 확인

service 를 실행하면 아래처럼 process 를 확인할 수 있다.


그리고 cmd 를 열어서 아래처럼 "docker run helloworld"를 쳐보면 된다.


Image Path

Settings > Advanced > Disk Image Location 에 가면 docker image 의 path 를 확인할 수 있다.

See Also

  1. 쿠...sal: [컴] Docker 설치 후 VMbox 가 안되는 경우

References

  1. Install Docker for Windows | Docker Documentation


[컴] MIT open courses

mooc / mit open courses / mit 오픈 코스들 / 컴퓨터 사이언스 코스 / 동영상 강의

MIT Open courses














See Also

  1. Stanford C programming
  2. 500 Free Computer Science Courses from the World’s Top CS Universities
  3. 150+ Stanford On-Campus Computer Science Courses Available Online : 스탠포드 대학 강의들을 주제별로 , 코스별로 묶어놨다.
  4. 쿠...sal: [컴][자료] Computer Science 관련 학습 자료

References

  1. MIT OCW : OCW Course Index | MIT OpenCourseWare | Free Online Course Materials

[컴] from php openssl to pycrypto (CTR mode, CBC mode)

php openssal to java abs cbc encrypt /

CTR mode from php openssl to pycrypto

php openssl ctr mode to python pycripto

To convert the php AES.CTR encryption to python AES.CTR encryption

php openssl code

$cipher = 'aes-256-ctr';

if (in_array($cipher, openssl_get_cipher_methods()))
{
    // Usually the below code is used for $iv
    // $ivlen = openssl_cipher_iv_length($cipher);
    // $iv = openssl_random_pseudo_bytes($ivlen);

    $key = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';

    // to start Counter from 1
    $iv0 = [00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,01];
    $iv = implode(array_map("chr", $iv0));

  
    $plaintext = 'test.test';
    $ciphertext = openssl_encrypt($plaintext, $cipher, $key, OPENSSL_RAW_DATA, $iv);
    $encodedCT = base64_encode($ciphertext); // $encodedCT => 'izQmJQ8nkPMP'
    
    //store $cipher, $iv, and $tag for decryption later    
    $original_plaintext = openssl_decrypt($encodedCT, $cipher, $key, 0, $iv);
    $original_plaintext2 = openssl_decrypt($ciphertext , $cipher, $key, OPENSSL_RAW_DATA, $iv);
}

python pycripto code


from Crypto.Cipher import AES
from Crypto.Util import Counter

import base64


def encrypt_token_ctr(data):
    key = b'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
    iv = 1

    # to convert string 'iv' to int value to use for initial_value
    # iv = b'AAAAAAAAAAAAAAAA'
    # import binascii
    # hexval = binascii.hexlify(iv)
    # iv = int(hexval, 16)

    aes = AES.new(key=key, 
        mode=AES.MODE_CTR, 
        counter=Counter.new(128, initial_value=iv)
    )

    return aes.encrypt(data.encode('utf8'))

def decrypt_token_ctr(data):
    key = b'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
    iv = 1

    aes = AES.new(key=key, 
        mode=AES.MODE_CTR, 
        counter=Counter.new(128, initial_value=iv)
    )

    return aes.decrypt(data)

print('Python encrypt: ' + str(base64.b64encode(encrypt_token_ctr('test.test'))) )
print('Python encrypt: ' + str(decrypt_token_ctr(base64.b64decode('izQmJQ8nkPMP'))) )


>> Python encrypt2: b'mzwsKw88h+c='
>> Python decrypt2: b'test.test'



AES CBC mode

php code


class AES
{
    var $key = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
    var $iv = 'AAAAAAAAAAAAAAAA';
 
    function encryptToken($data)
    {
        // Mcrypt library has been DEPRECATED since PHP 7.1, use openssl:
        // return openssl_encrypt($data, 'aes-256-cbc', $this->key, OPENSSL_RAW_DATA, $this->iv);

        $message_padded = $data;
        if (strlen($message_padded) % 8) {
            $message_padded = str_pad($message_padded,
                strlen($message_padded) + 8 - strlen($message_padded) % 8, "\0");
        }

        // $padding = 16 - (strlen($data) % 16);
        // $data .= str_repeat(chr($padding), $padding);
        return openssl_encrypt($message_padded, 'aes-256-cbc', $this->key, OPENSSL_RAW_DATA, $this->iv);
    }
 
    function decryptToken($data)
    {
        // Mcrypt library has been DEPRECATED since PHP 7.1, use openssl:
        // return openssl_decrypt(base64_decode($data), 'aes-256-cbc', $this->key, OPENSSL_RAW_DATA, $this->iv);
        $data = openssl_decrypt(base64_decode($data), 'aes-256-cbc', $this->key, OPENSSL_RAW_DATA, $this->iv);
        $padding = ord($data[strlen($data) - 1]);
        return substr($data, 0, -$padding);
    }
}

python code


from Crypto.Cipher import AES

def _pad(s): return (s + (AES.block_size - len(s) % AES.block_size) * chr(AES.block_size - len(s) % AES.block_size)).encode('utf8')

def _cipher():
    key = b'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
    iv = b'AAAAAAAAAAAAAAAA'
    return AES.new(key=key, mode=AES.MODE_CBC, IV=iv)


def encrypt_token(data):
    return _cipher().encrypt(_pad(data))

if __name__ == '__main__':
    print('Python encrypt: ' + str(base64.b64encode(encrypt_token('test.test'))) )
    print('Python decrypt: ' + str(decrypt_token(base64.b64decode('1pRLMUTqMCdBM4EyNyBTuQ=='))) )



See Also

  1. Encrypt and decrypt content with Nodejs · Christoph Hartmann
  2. AES/CBC/PKCS5Padding encrypt/decrypt PHP and JAVA example classes : java code <--> php code





[컴][알고리즘] AES 암호화 설명






  •  key expansion 은  key 를 4-byte 씩 쪼개어서 array 에 담는것
  • 16-byte 씩 작업을 하게 된다. 4 x 4 의 matrix 이다. 이 하나의 matrix을 state 라 부른다. 아래그림 참조
  • 각 정해진 bit 마다 round 가 정해져 있다.(128bit 은 10round, 256bit 는 14 round)
    • 각 bit 의 암호마다 "key-length/block-size/rounds" 수가 정해져 있다.
  • 이 (round-1)수 만큼 loop 을 돈다. 그리고 마지막 round 만 따로 한다.
  • 마지막 round 는 mix-columns 작업을 하지 않는다.

AES encryption process

1. SubBytes

sub 가 substitute 라고 보면 된다. State 에 있는 각 위치의 byte 에 해당하는 값을 "특정 table" 의 값과 교환하게 된다. 이 table 은 아래처럼 정해져있다.

여기 를 보면 어떻게 SubBytes 를 하는지 알 수 있다.

S = [ 
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01,
0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, 0xca, 0x82, 0xc9, 0x7d,
0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4,
0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc,
0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, 0x04, 0xc7,
0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2,
0xeb, 0x27, 0xb2, 0x75, 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e,
0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb,
0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, 0xd0, 0xef, 0xaa, 0xfb,
0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c,
0x9f, 0xa8, 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5,
0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, 0xcd, 0x0c,
0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d,
0x64, 0x5d, 0x19, 0x73, 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a,
0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3,
0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, 0xe7, 0xc8, 0x37, 0x6d,
0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a,
0xae, 0x08, 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6,
0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, 0x70, 0x3e,
0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9,
0x86, 0xc1, 0x1d, 0x9e, 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9,
0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99,
0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 ]
from: https://www.youtube.com/watch?v=gP4PqVGudtg

2. Shift Rows

아래처럼 각 row 를 shift 한다. 0번째 열은 '0'번, 1번째 열은 '1'번 ...
from: https://www.youtube.com/watch?v=2y_tidbY-Lw

3. MixColumns

여기 를 보면 animation 을 볼 수 있다.

state 의 각 column 을 Galois field 와 곱하기(matrix 곱하기) 를 하게 된다. 만약 GF(255) 라면 0~255 사이의 숫자로만 구성된 field(matrix) 가 만들어진다.



4. Add Round Key

"State 의 각 byte" 와 "Round key 의 각 byte" 을 XOR 한다.
from: https://www.youtube.com/watch?v=2y_tidbY-Lw



CTR mode 사용시 유의할 점

  • PHP data encryption primer > Encryption algorithm / mode of operation / nonce (initializing vector)
    • single key 를 사용하는 경우는 nonce 가 unique 해야만 안전함을 보장한다.
    • ...



[컴][OS] Cent OS 7 / apache / php7.2 설치




Download Minimal CentOS

Network configuration

install 시에 설정을 못했다면 if 파일을 수정하면 된다. 

vi /etc/sysconfig/network-scripts/ifcfg-enp0s3

ONBOOT=yes


repository 설정


rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

apache 설치

yum --enablerepo=remi,remi-php72 install httpd php php-common
php72 를 위해서는 remi 를 빼고 remi-php72 만 사용하자.

php 설치

yum --enablerepo=remi-php72 install php
위 link 를 참고해서 아래에서 필요한 부분만 골라서 설치하면 된다.
yum --enablerepo=remi,remi-php72 install php-pecl-apcu php-cli php-pear php-pdo php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml php-mysqlnd php-pgsql php-pecl-mongodb

php.ini

기본적으로 path 는 /etc/php.ini 로 되어 있다. php compile 시에 configure option 에 따라 기본 path 는 변경될 수 있다.(참고)
'--with-config-file-path=/etc/httpd/conf'

test.php

<?php
    phpinfo();
위 처럼 test.php 를 만들고 '/var/www/html/test.php' 에 놓은 후 접속하면 관련 설정 내역을 확인할 수 있다.

참고, apache 의 php 모듈 설정

  • apache config file path : /etc/httpd/conf/httpd.conf
httpd.conf 에 보면 LoadModule 부분이 아래처럼 되어 있다.(apache 2.4.6 기준)
Include conf.modules.d/*.conf 
그래서 php module 의 load 가 15-php.conf 안에 들어있다.(이것은 아마도 yum install 시에 자동으로 넣어주지 않을까 생각된다. 확인필요)
  • etc/httpd/conf/conf.modules.d/15-php.conf
  • /usr/lib64/httpd/modules/libphp7.so
여하튼, 그래서 추가설정 없이 바로 apache 에서 php 를 사용할 수 있다.



Dev. Enviroment

X Window System

아래처럼 하면 설치된다.
  1. yum groupinstall "X Window System" "Fonts"
  2. yum install gnome-classic-session gnome-terminal nautilus-open-terminal control-center liberation-mono-fonts
  3. unlink /etc/systemd/system/default.target
  4. ln -sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target
  5. reboot

Samba client / mount

yum install samba-client
smbclient -L 10.0.2.2 -U user_name -m SMB2
자세한 건 아래 링크를 참조하자.

ifconfig

ip addr






[컴][웹] 서브도메인 설정 방법

subdomain / how to set sub domain


서브 도메인 설정 방법

A record 추가

자신이 사용하는 DNS 로 가서 A record 를 추가한다. 아래의 newtest.testdomain.com

A newtest.testdomain.com 124.138.223.234
A testdomain.com         124.138.223.234

cloudflare

cloudflare 의 DNS 서버를 사용하는 경우 주의할 점은 "Status" 부분이다. 기본적으로 grey cloud 나 not use 로 해주자. 그렇지 않으면 cloudflare 에 의해 엉뚱한 ip address 에 묶여 있다.

Web server 설정

자신이 사용하는 web server 에서 설정을 해준다.

만약 apache 를 사용한다면 vhost.conf 등에서 아래처럼 새로운 ServerName 을 이용해서 <Virtualhost> 를 추가 하면 된다.
Listen 8080
<Virtualhost *:8080>
    ServerAdmin webmaster@localhost
    DocumentRoot /home/public
    ServerName newtest.testdomain.com   
    ... 


방화벽 설정

당연한 이야기지만 방화벽이 걸려있으면 같이 열어줘야 한다.