💬

SBI証券のパスワードを生成する

2024/02/04に公開

パスワード変更のお知らせ

SBI証券から4月までにパスワードを変更を促す通知が来ました。
新たなパスワードの条件が従来と違うので生成プログラムを作成してみました。

SBI証券のパスワード設定ルール

  • 10文字以上20文字以内
  • 下記3種類の文字を全て使用
    (1) 半角英字(1文字以上)
    (2) 半角数字(1文字以上)
    (3) 記号2種類以上
#! /usr/bin/env ruby
#
# sbi-password.rb
# 
def sbi_password(len = 10)
  len = 4 if len < 4
  alpha_l = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
  alpha_s = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
  alpha = alpha_l + alpha_s
  num = ["0","1","2","3","4","5","6","7","8","9"]
  sign = ["!",'"',"&","+",",",":","?","@","^","}","~"]
  sign_sub = []
  r = Random.new
  pw = ""
  num_count = 1		# 半角数字(1文字以上)
  sign_count = 2	# 記号2種類以上
  # 一文字目は英字
  pw << alpha[r.rand(0...alpha.size)]
  for i in 1...len
    ns_count = num_count + sign_count
    if i < (len - ns_count)
      str_type = r.rand(0..2)
      case str_type
      when 0	# 半角英字
        pw << alpha[r.rand(0...alpha.size)]
      when 1	# 半角数字
        pw << num[r.rand(0...num.size)]
        num_count = num_count - 1  if 0 < num_count
      when 2	# 記号
        pw, sign_count, sign_sub = random_sign(pw, sign_count, sign, sign_sub)
      end
    else
      # パスワード設定ルールのため強制的に半角数字または記号を生成
      if 0 < num_count		# 半角数字
        pw << num[r.rand(0...num.size)]
        num_count = num_count - 1
      elsif 0 < sign_count	# 記号
        pw, sign_count, sign_sub = random_sign(pw, sign_count, sign, sign_sub)
      end
    end
  end
  return pw
end

def random_sign(pw, sign_count, sign, sign_sub)
  r = Random.new
  if 2 == sign_count
    sign_c = sign[r.rand(0...sign.size)] 
    pw << sign_c
    sign_sub = sign.reject {|v| v == sign_c}
  elsif 1 == sign_count
    pw << sign_sub[r.rand(0...sign_sub.size)]
  else
    pw << sign[r.rand(0...sign.size)]
  end
  sign_count = sign_count - 1 if 0 < sign_count
  return pw, sign_count, sign_sub
end

Discussion