ECDSA

ECDSA(Elliptic Curve Digital Signature Algorithm)
楕円曲線デジタル署名アルゴリズム
ビットコインの電子証明などに利用されている
ECDSAは公開鍵の暗号化技術の名前
RSAに比べ約1/10のコンパクトな容量

require 'ecdsa'
require 'securerandom'

group = ECDSA::Group::Secp256k1
n = group.order

private_key = 1 + SecureRandom.random_number(n-1)

print private_key

vagrant@vagrant-ubuntu-trusty-64:~/bitcoin$ ruby main.rb
106515493506888278075922016172190811663525618981292515102301206639235796458599

ビットコインの暗号技術

OpenSSL
-> インターネット上で標準的に利用される暗号通信プロトコルであるSSLおよびTLSの機能を実装したオープンソースライブラリ

require 'openssl'

data = '*secret data*'

key = 'a' * 32
iv = 'i' * 16

enc = OpenSSL::Cipher.new('AES-256-CBC')
enc.encrypt
enc.key = key
enc.iv = iv
encrypted_data = enc.update(data) + enc.final
# print encrypted_data

dec = OpenSSL::Cipher.new('AES-256-CBC')
dec.decrypt
dec.key = key
dec.iv = iv 
decrypted_data = dec.update(encrypted_data) + dec.final
print decrypted_data

vagrant@vagrant-ubuntu-trusty-64:~/bitcoin$ ruby main.rb
5????
g`??Ա?

vagrant@vagrant-ubuntu-trusty-64:~/bitcoin$ ruby main.rb
*secret data*

IntelのIvy Bridge以降のCPUやRaspberry Pi, Apple A7以降のプロセッサには、熱雑音を利用して物理的に乱数を発生させるハードウェア乱数生成器が備わっている

rubyの安全な乱数: securerandom

require 'securerandom'
hex = SecureRandom.hex(32)
print hex

vagrant@vagrant-ubuntu-trusty-64:~/bitcoin$ ruby main.rb
1fbd11b0d5588de19571e3679ee86387d2449b6d376f6211498e4dbd2557fabd

bitcoin-ruby

$ sudo apt-get install git gcc build-essential libreadline-dev zlib1g-dev
$ sudo apt-get install libssl1.0-dev
$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
$ echo ‘export PATH=”$HOME/.rbenv/bin:$PATH”‘ >> ~/.bash_profile
$ echo ‘eval “$(rbenv init -)”‘ >> ~/.bash_profile
$ exec $SHELL -l

vagrant@vagrant-ubuntu-trusty-64:~/bitcoin$ rbenv -v
rbenv 1.1.2-11-gc46a970
vagrant@vagrant-ubuntu-trusty-64:~/bitcoin$ rbenv install 2.0.0-p643
Downloading ruby-2.0.0-p643.tar.bz2…
-> https://cache.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p643.tar.bz2
Installing ruby-2.0.0-p643…

WARNING: ruby-2.0.0-p643 is past its end of life and is now unsupported.
It no longer receives bug fixes or critical security updates.

Installed ruby-2.0.0-p643 to /home/vagrant/.rbenv/versions/2.0.0-p643

vagrant@vagrant-ubuntu-trusty-64:~/bitcoin$ rbenv rehash
vagrant@vagrant-ubuntu-trusty-64:~/bitcoin$ rbenv versions
* system (set by /home/vagrant/.rbenv/version)
2.0.0-p643
vagrant@vagrant-ubuntu-trusty-64:~/bitcoin$ rbenv global 2.0.0-p643
vagrant@vagrant-ubuntu-trusty-64:~/bitcoin$ ruby -v
ruby 2.0.0p643 (2015-02-25 revision 49749) [x86_64-linux]

なんかエラーが出ている

[vagrant@localhost rails]$ gem install pg -v ‘1.1.4’
Building native extensions. This could take a while…
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.

current directory: /home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/pg-1.1.4/ext
/home/vagrant/.rbenv/versions/2.3.1/bin/ruby -I /home/vagrant/.rbenv/versions/2.3.1/lib/ruby/site_ruby/2.3.0 -r ./siteconf20190809-24267-2gatu1.rb extconf.rb
checking for pg_config… no
No pg_config… trying anyway. If building fails, please try again with
–with-pg-config=/path/to/pg_config
checking for libpq-fe.h… no
Can’t find the ‘libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.

Provided configuration options:
–with-opt-dir
–without-opt-dir
–with-opt-include
–without-opt-include=${opt-dir}/include
–with-opt-lib
–without-opt-lib=${opt-dir}/lib
–with-make-prog
–without-make-prog
–srcdir=.
–curdir
–ruby=/home/vagrant/.rbenv/versions/2.3.1/bin/$(RUBY_BASE_NAME)
–with-pg
–without-pg
–enable-windows-cross
–disable-windows-cross
–with-pg-config
–without-pg-config
–with-pg_config
–without-pg_config
–with-pg-dir
–without-pg-dir
–with-pg-include
–without-pg-include=${pg-dir}/include
–with-pg-lib
–without-pg-lib=${pg-dir}/lib

To see why this extension failed to compile, please check the mkmf.log which can be found here:

/home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-linux/2.3.0-static/pg-1.1.4/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/pg-1.1.4 for inspection.
Results logged to /home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-linux/2.3.0-static/pg-1.1.4/gem_make.out
[vagrant@localhost rails]$ find ~/.rbenv | grep mkmf.log
/home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-linux/2.3.0-static/msgpack-1.3.1/mkmf.log
/home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-linux/2.3.0-static/pg-1.1.4/mkmf.log
/home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-linux/2.3.0-static/nio4r-2.4.0/mkmf.log
/home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-linux/2.3.0-static/ffi-1.11.1/mkmf.log
/home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-linux/2.3.0-static/nokogiri-1.10.3/mkmf.log
[vagrant@localhost rails]$ cat /home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-linux/2.3.0-static/pg-1.1.4/mkmf.log
find_executable: checking for pg_config… ——————– no

——————–

find_header: checking for libpq-fe.h… ——————– no

“gcc -o conftest -I/home/vagrant/.rbenv/versions/2.3.1/include/ruby-2.3.0/x86_64-linux -I/home/vagrant/.rbenv/versions/2.3.1/include/ruby-2.3.0/ruby/backward -I/home/vagrant/.rbenv/versions/2.3.1/include/ruby-2.3.0 -I. -I/home/vagrant/.rbenv/versions/2.3.1/include -O3 -fno-fast-math -ggdb3 -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration -Wdeprecated-declarations -Wno-packed-bitfield-compat conftest.c -L. -L/home/vagrant/.rbenv/versions/2.3.1/lib -Wl,-R/home/vagrant/.rbenv/versions/2.3.1/lib -L. -L/home/vagrant/.rbenv/versions/2.3.1/lib -fstack-protector -rdynamic -Wl,-export-dynamic -Wl,-R/home/vagrant/.rbenv/versions/2.3.1/lib -L/home/vagrant/.rbenv/versions/2.3.1/lib -lruby-static -lpthread -lrt -ldl -lcrypt -lm -lc”
checked program was:
/* begin */
1: #include “ruby.h”
2:
3: int main(int argc, char **argv)
4: {
5: return 0;
6: }
/* end */

“gcc -E -I/home/vagrant/.rbenv/versions/2.3.1/include/ruby-2.3.0/x86_64-linux -I/home/vagrant/.rbenv/versions/2.3.1/include/ruby-2.3.0/ruby/backward -I/home/vagrant/.rbenv/versions/2.3.1/include/ruby-2.3.0 -I. -I/home/vagrant/.rbenv/versions/2.3.1/include -O3 -fno-fast-math -ggdb3 -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration -Wdeprecated-declarations -Wno-packed-bitfield-compat conftest.c -o conftest.i”
conftest.c:3:22: error: libpq-fe.h: そのようなファイルやディレクトリはありません
checked program was:
/* begin */
1: #include “ruby.h”
2:
3: #include /* end */

——————–
え?

bundle install

[vagrant@localhost ruby3]$ bundle install
Fetching gem metadata from https://rubygems.org/...........
Fetching version metadata from https://rubygems.org/.
Using json 1.8.3
Installing multi_xml 0.5.5
Installing rack 1.6.4
Installing tilt 2.0.2
Using bundler 1.13.6
Installing httparty 0.13.7
Using rack-protection 1.5.3
Using sinatra 1.4.7
Bundle complete! 3 Gemfile dependencies, 8 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.
Post-install message from httparty:
When you HTTParty, you must party hard!
get '/product' do
	...
	@products = []
	LOCATIONS.each do |location|
	 @product.push DATA.select { |product| product['location'] == location }.sample
end
<% @product.each do |product| %>
<a href='/products/location<%= product&#91;location&#93; %>'>
<div class='product'>
	<div class='thumb'>
		<img src='<%= product&#91;'url'&#93; %>'>
	</div>
	<div class='caption'>
		<%= product&#91;'location'&#93; != 'us' ? product&#91;'location'&#93;.capitalize : product&#91;'location'&#93;.upcase %>
		</div>
	</div>
</a>
<% end %>
get '/products/location/:location' do
	DATA = HTTParty.get('https://fomotograph-api.udacity.com/products.json')['photos']
	@products = DATA.select{ |product| product['location'] == params[:location]}
	erb "<!DOCTYPE html>..."
end


get '/products/:id' do
	DATA = HTTPartyp.get('https://hogehoge/products.json')['photos']
	@product = DATA.select{ |prod| prod['id'] == params[:id].to_i }.first
	erb "<!DOCTYPE html> ..."
end	
class Product

	url = 'https://fomotograph-api.com/product.json'
	DATA = HTTParty.get(url)['photos']
end
require 'HTTParty'
require 'json'

class Product

	url = 'https://fomotograph-api.com/product.json'
	DATA = HTTParty.get(url)['photos']

	def initialize(product_data = {})
	@id = product_data['id']
	@title = product_data['title']
	@location = product_data['location']
	@summary = product_data['summary']
	@url = product_data['url']
	@price = product_data['price']
	end

	def self.all
		DATA.map { |product| new(product) }
	end

	def self.sample_locations
		@products = []
		LOCATIONS.each do |location|
		@products.push self.all.select { |product| product.location == location }.sample_locations
		end
		return @product
	end

	def self.find_by_location(location)
		self.all.select { |product| product.location == location }
	end
end
{
	"id"=>27, "title"=>"worlds end", "summary"=>"travel tothe end ...",
	"location"=>"scotland", "price"=>37, "url"=>"/images/scotland/worlds-end.jpg"
}

controller

get '/products' do
	@products = Product.all
	erb :products
end

rpsecによるリファクタリング

require 'calc'

RSpec.describe "A calc" do
  before do
    @calc = Calc.new
  end
  it "given 2 and 3, returns 5" do
    expect(@calc.add(2, 3)).to eq(5)
  end
  it "given 5 and 8, returns 13" do
    expect(@calc.add(5, 8)).to eq(13)
  end
end
class Calc
  def add(a, b)
    a + b # 仮実装
  end
end
[vagrant@localhost rspec]$ rspec
..

Finished in 0.00235 seconds (files took 0.11087 seconds to load)
2 examples, 0 failures

describeはcontextを書き換えることも可能。また、コマンドラインはrspec -fdとしても使われる。

pendding

require 'calc'

RSpec.describe Calc do
  describe "when normal mode" do
  it "given 2 and 3, returns 5" do
    calc = Cal.new
    expect(calc.add(2, 3)).to eq(5)
  end
  describe "when graph mode" do
  it "draws graph" do
  end
end

matcher:https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers

    expect(calc.add(2, 3)).to eq(5) #matcher
    expect(calc.add(2, 3)).not_to eq(5) #matcher
    expect(calc.add(2, 3)).to be true #matcher
    expect(calc.add(2, 3)).to be false #matcher
    expect(calc.add(2, 3)).to be > 10 #matcher
    expect(calc.add(2, 3)).to be_between(1, 10).include #matcher
    expect(calc).to respond_to(:add) #matcher
    expect(calc.add(2, 3).integer?).to be true

subject

require 'calc'

RSpec.describe Calc do
  subject(:calc){ Calc.new }
  it {
    # calc = Calc.new
    expect(calc.add(2, 3)).to eq(5)
  }
end

let

context "tax 5%" do
    let(:tax){ 0.05 }
    it { expect(calc.price(100, tax)).to eq(105)}
  end
  context "tax 8%" do
    let(:tax){ 0.08 }
    it { expect(calc.price(100, tax)).to eq(108)}
  end

message expectation

class Calc

  def initialize(logger)
    @logger = logger
  end

  def add(a, b)
    @logger.log
    a + b
  end

end

rspecによるBDD

rspec install

[vagrant@localhost rspec]$ gem install rspec
[vagrant@localhost rspec]$ ruby -v
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
[vagrant@localhost rspec]$ rspec -v
3.5.4

TDD/BDD 開発サイクル
1.失敗するテストを書く(Red)
2.最小限のコードを書いてテストをパスさせる(Green)
3.リファクタリング

initフォルダの作成

[vagrant@localhost rspec]$ rspec --init
  create   .rspec
  create   spec/spec_helper.rb

テストファイルは〇〇_spec.rbとし、specフォルダの中に書きます。

1.ミスするコード

RSpec.describe "A calc" do
  it "given 2 and 3, return 5" do
    calc = Calc.new
    epect(calc.add(2, 3)).to eq(5)
  end
end
[vagrant@localhost rspec]$ rspec
F

Failures:

  1) A calc given 2 and 3, return 5
     Failure/Error: calc = Calc.new

     NameError:
       uninitialized constant Calc
     # ./spec/calc_spec.rb:3:in `block (2 levels) in '

Finished in 0.00317 seconds (files took 0.32741 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/calc_spec.rb:2 # A calc given 2 and 3, return 5

2.パスするコードを作成

class Calc
  def add(a, b)
    5 # 仮実装
  end
end
require 'calc'

RSpec.describe "A calc" do
  it "given 2 and 3, returns 5" do
    calc = Calc.new
    expect(calc.add(2, 3)).to eq(5)
  end
end

実行

[vagrant@localhost rspec]$ rspec
.

Finished in 0.00095 seconds (files took 0.10059 seconds to load)
1 example, 0 failures

ruby Sinatra 掲示板(bbs)

掲示板の作成です。DBにSQlite3、Deleteにjqueryを使っています。

C:/rails/sinatra/main.rb

require 'sinatra'
require 'sinatra/reloader'
require 'active_record'

ActiveRecord::Base.establish_connection(
	"adapter" => "sqlite3",
	"database" => "./bbs.db"
)

helpers do
	include Rack::Utils
	alias_method :h, :escape_html
end

class Comment < ActiveRecord::Base
end

get '/'  do
	@comments = Comment.order("id desc").all
		erb :index
end

post '/new'  do
	Comment.create({:body => params[:body]})
	redirect '/'
end

post '/delete'  do
	Comment.find(params[:id]).destroy
end

C:/rails/sinatra/views/index.erb

<!DOCTYPE>
<html lang="ja">
<head>
	<meta charset="utf-8">
	<title>BBS</title>
</head>
<body>
<h1>BBS</h1>
<ul>
	<% @comments.each do |comment| %>
	<li data-id="<%= comment.id %>">
	<%= h comment.body %>
		<span class="deleteCmd" style="cursor:pointer;color:blue">[x]</span>
	</li>
	<% end %>
</ul>
	<h2>Add New</h2>
	<form method="post" action="/new">
		<input type="text" name="body"><input type="submit" value="post!">

		<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
		<script>
		$('.deleteCmd').click(function(){
			var el = $(this).parent();
			if (confirm('are you sure to delete?')){
				$.post('/delete', {
					id: el.data('id')
				}, function(){
					el.fadeOut(800);
				});
			}
		})
		</script>
</body>
</html>

C:/rails/sinatra/import.sql

create table comments (
 id integer primary key,
 body text
);

ブラウザで確認してみてください。
bbs

ruby Sinatra

sinatraはrubyのフレームワークで、簡単にwebアプリケーションを作ることができます。

shinatra

まずは、rubyのバージョンから

C:\rails\sinatra>ruby -v
ruby 2.3.1p112 (2016-04-26 revision 54768) [x64-mingw32]

C:\rails\sinatra>gem -v
2.5.1

C:\rails\sinatra>gem list activerecord

*** LOCAL GEMS ***

activerecord (5.0.0.1, 4.2.7.1)

C:\rails\sinatra>gem list sqlite3

*** LOCAL GEMS ***

sqlite3 (1.3.12 x64-mingw32)

そして、gem。sinatra-contribは、サーバー再起動を手間を省くために、インストールします。

C:\rails\sinatra>gem list sinatra

*** LOCAL GEMS ***

sinatra (1.4.7)
sinatra-contrib (1.4.7)

インストールしたら、早速、ファイルを作成して、ローカル環境(http://localhost:4567/)でテストしてみましょう。
C:/rails/sinatra/main.rb

require 'sinatra'
require 'sinatra/reloader'

get '/'  do
	erb :index
end

C:/rails/sinatra/views/index.erb

<!DOCTYPE>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>sinatra</title>
</head>
<body>
<h1>hello world</h1>
</body>
</html>

ブラウザで確認します。
%e7%84%a1%e9%a1%8c

railsで遊ぼう application.html.erbエラー

新規にファイルを作った際に、window環境で、
application.html.erb に関して
「ActionView::Template::Error (TypeError: オブジェクトでサポートされていないプロパティまたはメソッドです。」と表示されることがあります。

/hoge/views/layouts/application.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title>Myapp</title>
    <%= csrf_meta_tags %>

    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
  </head>

  <body>
    <%= yield %>
  </body>
</html>

この場合、line7, 8 の’application’を’default’に変更すると、エラーが消えます。

<!DOCTYPE html>
<html>
  <head>
    <title>Myapp</title>
    <%= csrf_meta_tags %>

    <%= stylesheet_link_tag    'default', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'default', 'data-turbolinks-track': 'reload' %>
  </head>

  <body>
    <%= yield %>
  </body>
</html>

参考:stack overflow:ProgramError