checksum 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #!/usr/bin/env bash
  2. __rvm_md5_calculate()
  3. {
  4. rvm_debug "Calculate md5 checksum for $@"
  5. \typeset _sum
  6. if
  7. builtin command -v md5 > /dev/null 2>&1
  8. then
  9. _sum=$(md5 "$@")
  10. echo ${_sum##* }
  11. return 0
  12. elif
  13. builtin command -v md5sum > /dev/null 2>&1
  14. then
  15. _sum=$(md5sum "$@")
  16. echo ${_sum%% *}
  17. return 0
  18. elif
  19. builtin command -v gmd5sum > /dev/null 2>&1
  20. then
  21. _sum=$(gmd5sum "$@")
  22. echo ${_sum%% *}
  23. return 0
  24. else
  25. for _path in /usr/gnu/bin /opt/csw/bin /sbin /bin /usr/bin /usr/sbin
  26. do
  27. if
  28. [[ -x "${_path}/md5" ]]
  29. then
  30. _sum=$(${_path}/md5 "$@")
  31. echo ${_sum##* }
  32. return 0
  33. elif
  34. [[ -x "${_path}/md5sum" ]]
  35. then
  36. _sum=$(${_path}/md5sum "$@")
  37. echo ${_sum%% *}
  38. return 0
  39. elif
  40. [[ -x "${_path}/gmd5sum" ]]
  41. then
  42. _sum=$(${_path}/gmd5sum "$@")
  43. echo ${_sum%% *}
  44. return 0
  45. fi
  46. done
  47. fi
  48. rvm_error "Neither of md5sum, md5, gmd5sum found in the PATH"
  49. return 1
  50. }
  51. __rvm_sha__calculate()
  52. {
  53. rvm_debug "Calculate sha512 checksum for $@"
  54. \typeset bits _sum
  55. bits=${1:-512}
  56. shift
  57. if
  58. builtin command -v sha${bits}sum >/dev/null
  59. then
  60. _sum=$(sha${bits}sum "$@")
  61. echo ${_sum% *}
  62. return 0
  63. elif
  64. builtin command -v sha${bits} >/dev/null
  65. then
  66. _sum=$(sha${bits} "$@")
  67. if [[ "${_sum%% *}" == "SHA${bits}" ]]
  68. then echo ${_sum##* } # take last of: SHA512 (file) = ....
  69. else echo ${_sum% *} # take first of: ... file
  70. fi
  71. return 0
  72. elif
  73. builtin command -v shasum >/dev/null
  74. then
  75. _sum=$(shasum -a${bits} "$@")
  76. echo ${_sum% *}
  77. return 0
  78. elif
  79. # Drop-in for Solaris 10
  80. builtin command -v /opt/csw/bin/shasum >/dev/null
  81. then
  82. _sum=$(/opt/csw/bin/shasum -a${bits} "$@")
  83. echo ${_sum% *}
  84. return 0
  85. fi
  86. rvm_error "Neither sha512sum nor shasum found in the PATH"
  87. return 1
  88. }
  89. # __rvm_checksum_calculate_file {file}
  90. # ENV out: _checksum_md5 _checksum_sha512
  91. __rvm_checksum_calculate_file()
  92. {
  93. rvm_debug "Calculate checksums for file ${1}"
  94. _checksum_md5="$( __rvm_md5_calculate "${1:-}" )"
  95. _checksum_sha512="$( __rvm_sha__calculate 512 "${1:-}" )"
  96. }
  97. __rvm_checksum_none()
  98. {
  99. [[ -z "${_checksum_md5:-}" && -z "${_checksum_sha512:-}" ]]
  100. }
  101. __rvm_checksum_any()
  102. {
  103. [[ -n "${_checksum_md5:-}" || -n "${_checksum_sha512:-}" ]]
  104. }
  105. __rvm_checksum_all()
  106. {
  107. [[ -n "${_checksum_md5:-}" && -n "${_checksum_sha512:-}" ]]
  108. }
  109. # __rvm_checksum_validate_file {file}
  110. # ENV in: _checksum_md5 _checksum_sha512
  111. __rvm_checksum_validate_file()
  112. {
  113. rvm_debug "Validating checksums for file ${1}"
  114. if
  115. __rvm_checksum_any
  116. then
  117. rvm_debug " ...checksums found in db"
  118. else
  119. rvm_debug " ...checksums not found in db"
  120. return 1
  121. fi
  122. # Validate md5 checksum
  123. if
  124. [[ -n "${_checksum_md5:-}" ]]
  125. then
  126. rvm_debug "Validating md5 checksum"
  127. if
  128. [[ "$(__rvm_md5_calculate "${1:-}")" == "${_checksum_md5:-}" ]]
  129. then
  130. rvm_debug " ...md5 checksum is valid!"
  131. else
  132. rvm_debug " ...md5 checksum is not valid!!!"
  133. return 2
  134. fi
  135. fi
  136. # Validate sha512 checksum
  137. if
  138. [[ -n "${_checksum_sha512:-}" ]]
  139. then
  140. rvm_debug "Validating sha15 checksum"
  141. if
  142. [[ "$(__rvm_sha__calculate 512 "${1:-}")" == "${_checksum_sha512:-}" ]]
  143. then
  144. rvm_debug " ...sha512 checksum is valid!"
  145. else
  146. rvm_debug " ...sha512 checksum is not valid!!!"
  147. return 3
  148. fi
  149. fi
  150. return 0
  151. }
  152. # __rvm_checksum_read {name...}
  153. # name - list of names to check, like:
  154. #
  155. # __rvm_checksum_read https://rvm.io/binaries/.../ruby-1.9.3-p194.tar.bz2 bin-ruby-1.9.3-p194.tar.bz2
  156. #
  157. # ENV in/out: _checksum_md5 _checksum_sha512
  158. __rvm_checksum_read()
  159. {
  160. rvm_debug "Load checksums for $1"
  161. __rvm_checksum_any && return 0
  162. \typeset _type _value _name
  163. \typeset -a _list
  164. list=()
  165. for _name in "$@"
  166. do
  167. if
  168. [[ "$_name" == *"?"* ]] # try url without ?... like ?rvm={version}
  169. then
  170. list+=( "${_name%\?*}" )
  171. else
  172. list+=( "$_name" )
  173. fi
  174. done
  175. for _name in "${list[@]}"
  176. do
  177. rvm_debug "Searching checksum config files for $_name"
  178. # md5
  179. _checksum_md5="$( __rvm_db_ "$rvm_path/config/md5" "$_name" | \command \head -n 1 )"
  180. [[ -n "${_checksum_md5:-}" ]] ||
  181. _checksum_md5="$( __rvm_db_ "$rvm_user_path/md5" "$_name" | \command \head -n 1 )"
  182. # sha512
  183. _checksum_sha512="$( __rvm_db_ "$rvm_path/config/sha512" "$_name" | \command \head -n 1 )"
  184. [[ -n "${_checksum_sha512:-}" ]] ||
  185. _checksum_sha512="$( __rvm_db_ "$rvm_user_path/sha512" "$_name" | \command \head -n 1 )"
  186. __rvm_checksum_any && return 0
  187. done
  188. # Try to get the Rubinius checksum from the network
  189. for _name in "${list[@]}"
  190. do
  191. if [[ $_name == http*rubinius* ]]; then
  192. if [[ -z "${_checksum_md5:-}" ]]; then
  193. _checksum_md5="$(__rvm_curl -s -L $_name.md5)"
  194. fi
  195. if [[ -z "${_checksum_sha512:-}" ]]; then
  196. _checksum_sha512="$(__rvm_curl -s -L $_name.sha512)"
  197. fi
  198. fi
  199. __rvm_checksum_any && return 0
  200. done
  201. rvm_debug " ...checksums not found"
  202. return 1 # not found
  203. }
  204. # Record checksums in user settings
  205. # Usage: __rvm_checksum_write {name}
  206. # ENV in/out: _checksum_md5 _checksum_sha512
  207. __rvm_checksum_write()
  208. {
  209. [[ -n "${1:-}" ]] || return 1
  210. __rvm_checksum_any || return 1
  211. [[ -z "${_checksum_md5:-}" ]] || __rvm_db_ "$rvm_user_path/md5" "${1:-}" "${_checksum_md5:-}"
  212. [[ -z "${_checksum_sha512:-}" ]] || __rvm_db_ "$rvm_user_path/sha512" "${1:-}" "${_checksum_sha512:-}"
  213. return 0
  214. }